Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the font of a pagertitlestrip

Typeface xyz=Typeface.createFromAsset(view.getContext().getAssets(),
                "fonts/xyz.ttf");
(TextView)abc.setTypeface(xyz);

This is how you create and set a custom typeface.

I need to set the typeface/font of a pagertitlestrip but there is no .setTypeFace function for pagertitlestrip. Does anyone know how I can do that anyway?

like image 267
Drilon Berisha Avatar asked Dec 27 '22 16:12

Drilon Berisha


1 Answers

The PagerTitleStrip is actually extends ViewGroup, so you can just subclass it and iterate through each of its children to find the views that need their font to be changed:

public class CustomFontPagerTitleStrip extends PagerTitleStrip {
    public CustomFontPagerTitleStrip(Context context) {
        super(context);
    }
    public CustomFontPagerTitleStrip(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xyz.ttf");
        for (int i=0; i<this.getChildCount(); i++) {
            if (this.getChildAt(i) instanceof TextView) {
                ((TextView)this.getChildAt(i)).setTypeface(tf);
            }
        }
    }
}

The only downside is that it prevents Eclipse from being able to display your layout.

like image 100
jburns20 Avatar answered Jan 18 '23 15:01

jburns20