1

i am trying to set a custom TTF font inside my PreferenceActivity.

The preference activity contains 2 ListPreferences, nothing else. Inside my PreferenceActivity, i have this code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);
    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    Preference conPref = findPreference(key);

    // this works correctly, i'm able to get the correct text form tv.getText();        
    TextView tv = (TextView) conPref.getView(null, null).findViewById(android.R.id.title);

    Typeface gilFontBook = Typeface.createFromAsset(getAssets(), "fonts/gilbook.ttf");
    tv.setTypeface(gilFontBook);  // doesnt change the typeface
}

But this doesn't work form me. The fontface remains the same before and after selecting an item from the ListPreference

Dusan
  • 3,284
  • 6
  • 25
  • 46
  • Have you put your custom font in the right place? If yes, make sure that you have written the exact name of your font ( I mean maybe it's just `gilbook.ttf` without `fonts/`) – Rick Jan 31 '14 at 14:33
  • The font is in the right place. I can change the font this way for other TextViews and also Buttons. – Dusan Jan 31 '14 at 14:34

2 Answers2

0

As far as I know you've to extend the PreferenceActivity class to do that. This answer helped me:

https://stackoverflow.com/a/13193623/814353

Community
  • 1
  • 1
peitek
  • 884
  • 2
  • 17
  • 27
  • Just tried the code from the link. I'm able to change the font color as in the example, but not the TypeFace. I'm trying to change the typeface: private void setStyleWarning(View view) { TextView titleView = (TextView) view.findViewById(android.R.id.title); Typeface gilFontBook = Typeface.createFromAsset(this.getContext().getAssets(), "fonts/gilbook.ttf"); titleView.setTypeface(gilFontBook); } But i get a NullPointerException at the line, where the font is created from assets – Dusan Feb 01 '14 at 01:38
0

I did it the bruteforce way:

public class FontPreference extends Preference {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FontPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public FontPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

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

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

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        loopOverViewGroup(view);
    }

    private void loopOverViewGroup(View view){
        if(view instanceof ViewGroup){
            ViewGroup group = (ViewGroup)view;
            for(int i = 0; i < ((ViewGroup)view).getChildCount(); i++){
                View childView = ((ViewGroup)view).getChildAt(i);
                if(childView instanceof ViewGroup){
                    loopOverViewGroup(childView);
                }else{
                    setTypeFace(childView);
                }
            }
        }
    }

    private void setTypeFace(View view){
        if(view instanceof TextView){
            ((TextView)view).setTypeface(Typeface.createFromAsset(getContext().getAssets(), "YOURFONT.ttf"));
            ((TextView)view).setTextColor(getContext().getResources().getColor(Color.WHITE));
        }
    }
}
A.S.
  • 4,574
  • 3
  • 26
  • 43