4

In my app I'm using a ViewPager with a PagerTabStrip and I need to set custom fonts to my widgets. To set a font for a Button or a TextView I just extend the class and set a typeface.

public class MyButton extends Button {

    public MyButton(Context context) {
        super(context);
        initCustomFont();
    }

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

    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCustomFont();
    }

    private void initCustomFont() {
        if(!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/boton_regular.ttf");
            setTypeface(tf);
        }
    }
}

But I can't use this method for a PagerTabStrip. Is there another way to set a typeface that can be used with PagerTabStrip?

droid8421
  • 895
  • 1
  • 13
  • 26

2 Answers2

20

A little late but here is one solution. Get the child views of the PagerTabStrip and check if it is an instance of a TextView. If it is, set the typeface:

for (int i = 0; i < pagerTabStrip.getChildCount(); ++i) {
    View nextChild = pagerTabStrip.getChildAt(i);
    if (nextChild instanceof TextView) {
       TextView textViewToConvert = (TextView) nextChild;
       textViewToConvert.setTypeface(PUT_TYPEFACE_HERE)
    }
}
StackOverflower
  • 410
  • 3
  • 9
  • This is ugly but so far probably the quickest solution possible. Thanks =) – ben Dec 13 '12 at 03:13
  • I agree but I could not think of another solution other than grabbing the source code directly and modifying it. There is no supported api to directly change the typeface of a PagerTabStrip. – StackOverflower Jan 03 '13 at 21:52
1

Inspect the PagerTabStrip with Hierarchy View (in Eclipse open Hierarchy View perspective when you run the application) and search for its children. One must be a TextView that holds the tab's title. Get this TextView and set its typeface.

Andrei Catinean
  • 863
  • 10
  • 15