My PreferenceActivity looks like:
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.widget.TextView;
public class Settings extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.about);
Typeface timesFont = Typeface.createFromAsset(getAssets(), "fonts/times.ttf");
TextView about_txt = (TextView) findViewById(R.id.about_txt);
about_txt.setTypeface(timesFont);
about_txt.setText(R.string.about_txt);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB)
{
addPreferencesFromResource(R.xml.preferences);
} else {
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
}
}
public static class PrefsFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
And preference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:dialogTitle="@string/size_dialog"
android:entries="@array/size_list"
android:entryValues="@array/entry_size"
android:key="size_list"
android:summary="@string/size_summary"
android:title="@string/pref4title"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_about"
android:layout="@layout/about"
>
</PreferenceCategory>
</PreferenceScreen>
@layout/about (about.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/about_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text here"
/>
</LinearLayout>
I have specific language, so for text in about.xml I should use specific font. Regularly for TextView I use:
Typeface timesFont = Typeface.createFromAsset(getAssets(), "fonts/times.ttf");
TextView about_txt = (TextView) findViewById(R.id.about_txt);
about_txt.setTypeface(timesFont);
about_txt.setText(R.string.about_txt);
but what I should use to access to text in about.xml which add to prefernce screen ?
Thanks in advance!
The easiest way to change typeface for all
TextViews
inPreferenceScreen
is:That's it!
With the addition of font support in android O, we can now set a
font-family
to app theme and that would automatically reflect in thePreferenceFragment
as well. (Note: I am usingPreferenceFragmentCompat
). First add afont-family
to the resources like this:NOTE: You would need both
app
andandroid
namespace declarations as of now LinkThen add a
style
for the text view mentioning thisfont-family
for theTextView
:And then finally, just refer to the above
style
in yourAppTheme
like this:Done. This will show the customised font in your
PreferenceFragmentCompat
as well.Maybe more easy to write your own Preference and add it in xml
just set the custom font via Spannable to the settings fields:
(looks long but fast done :))
complete solution:
call it for every preference and you're done :)
have fun && good luck
btw: on changing text, call it again!!
btw2: checked, seen, applied, improved ;) from: https://stackoverflow.com/a/10741161/371749
btw3, nearly forgot: