set custom font for text in PreferenceScreen

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:

private void convertPreferenceToUseCustomFont(Preference somePreference) {
    CustomTypefaceSpan customTypefaceSpan = new CustomTypefaceSpan("", net.mikekober.myMory.utils.Utils.getUsedTypeFace(getActivity()));

    SpannableStringBuilder ss;
    if (somePreference.getTitle() != null) {
        ss = new SpannableStringBuilder(somePreference.getTitle().toString());
        ss.setSpan(customTypefaceSpan, 0, ss.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        somePreference.setTitle(ss);
    }

    if (somePreference.getSummary() != null) {
        ss = new SpannableStringBuilder(somePreference.getSummary().toString());
        ss.setSpan(customTypefaceSpan, 0, ss.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        somePreference.setSummary(ss);
    }
}

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:

static private class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}

public class CustomPreference extends Preference 
    implements PreferenceStyle {

    private int style;

    public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public CustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomPreference(Context context) {
        super(context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        switch (style) {
        case STYLE_ALARMED:
            setStyleAlarmed(view);
            break;
        case STYLE_NORMAL:
            setStyleNormal(view);
            break;
        case STYLE_WARNING:
            setStyleWarning(view);
            break;
        case STYLE_SUMMARY_ALARM:
            setStyleSummaryAlarm(view);
            break;
        case STYLE_SUMMARY_WARNING:
            setStyleSummaryWarning(view);
            break;
        case STYLE_DISABLED:
            setStyleDisabled(view);
            break;
        default:
            break;
        }

    }

private void setStyleWarning(View view) {
    TextView titleView = (TextView) view.findViewById(android.R.id.title);
    titleView.setTextColor(Color.YELLOW);
            // add your FONT here
}

private void setStyleAlarmed(View view) {
    int alarmREDColor = view.getContext().getResources().getColor(R.color.alarmed_red);
    TextView titleView = (TextView) view.findViewById(android.R.id.title);
    titleView.setTextColor(alarmREDColor);
}

public void setStyle(int style) {
        switch (style) {
        case STYLE_ALARMED:
            this.style = STYLE_ALARMED;
            break;
        case STYLE_NORMAL:
            this.style = STYLE_NORMAL;
            break;
        default:
            this.style = STYLE_NORMAL;
        }
    }

public interface PreferenceStyle {

    public static final int STYLE_DISABLED = 0;
    public static final int STYLE_NORMAL = 10;
    public static final int STYLE_ALARMED = 20;
    public static final int STYLE_WARNING = 30;

    public static final int STYLE_SUMMARY_ALARM = 40;
    public static final int STYLE_SUMMARY_WARNING = 50;

    public void setStyle(int style);
    public Context getContext();
    public void setSummary(CharSequence summary);

}

public class YourActivity extends PreferenceActivity {

    private CustomPreference pref1;
    private CustomPreference pref2;
    private CustomPreference pref3;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         
        addPreferencesFromResource(R.xml.prefence_xml);
        pref1 = findPreference();
                    pref1.setOnPreferenceClickListener(this);
                    pref1.setStyle(PreferenceStyle.STYLE_NORMAL);
               }
        }

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >


    <PreferenceCategory 
        android:title="category1">

        <com.project.main.preference.CustomPreference 
            android:key="key"
            android:title="title"
            />  

        <com.project.main.preference.CustomPreference 
            android:key="key"
            android:title="title"/>

    </PreferenceCategory>

</PreferenceScreen>

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 the PreferenceFragment as well. (Note: I am using PreferenceFragmentCompat). First add a font-family to the resources like this:

<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/nunito"

    app:fontStyle="normal"
    app:fontWeight="400"
    app:font="@font/nunito"/>

<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/nunito_italic"

    app:fontStyle="italic"
    app:fontWeight="400"
    app:font="@font/nunito_italic" />

NOTE: You would need both app and android namespace declarations as of now Link

Then add a style for the text view mentioning this font-family for the TextView:

<style name="NunitoTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">@font/nunito_regular_font_family</item>
</style>

And then finally, just refer to the above style in your AppTheme like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    ........
    <item name="android:textViewStyle">@style/NunitoTextViewStyle</item>
</style>

Done. This will show the customised font in your PreferenceFragmentCompat as well.


The easiest way to change typeface for all TextViews in PreferenceScreen is:

public class WallpaperSettings extends PreferenceActivity {

  private View tryInflate(String name, Context context, AttributeSet attrs) {
    LayoutInflater li = LayoutInflater.from(context);
    View v = null;
    try {
        v = li.createView(name, null, attrs); 
    } catch (Exception e) {
        try {
            v = li.createView("android.widget." + name, null, attrs);
        } catch (Exception e1) {
        }
    }
    return v;
  }

  protected void onCreate(Bundle savedInstanceState) {
    //CHANGE TYPEFACE FOR ALL TEXTVIEWS
    final Typeface font = Typeface.createFromAsset(getAssets(), "fonts/cabal.ttf");     
    getLayoutInflater().setFactory(new Factory() {
        @Override
        public View onCreateView(String name, Context context,
                AttributeSet attrs) {
            View v = tryInflate(name, context, attrs);
            if (v instanceof TextView) {                    
                ((TextView) v).setTypeface(font);
            }
            return v;
        }
    });

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);
  }
}

That's it!