Skip to content Skip to sidebar Skip to footer

Tabpageindicator Text Font

I'm using ViewPagerIndicator, and haven't had any luck, so I'll ask here. Is there a way to change the font for the Tabs in a TabPageIndicator?

Solution 1:

You don't need to use an other third party lib in order to do this, you can modify the TabView class(in TabPageIndicator) like this(this assuming you want the same font in your entire app):

privateclassTabViewextendsTextView {
    privateint mIndex;

    publicTabView(Context context) {
        super(context, null, R.attr.vpiTabPageIndicatorStyle);
    }

    @OverridepublicvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Re-measure if we went beyond our maximum size.if (mMaxTabWidth > 0 && getMeasuredWidth() > mMaxTabWidth) {
            super.onMeasure(MeasureSpec.makeMeasureSpec(mMaxTabWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
        }
    }

    publicintgetIndex() {
        return mIndex;
    }

    publicTabView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    publicTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    publicTabView(Context context) {
        super(context);
        init();
    }

    privatevoidinit() {

        Typefacetf= Typeface.createFromAsset(getContext().getAssets(), "fonts/myFont.ttf"); // setting a custom TypeFace
        setTypeface(tf);

    }

Solution 2:

There is no need to use a third-party lib nor to change ViewPagerIndicator lib.

TabPageIndicator has a single ViewGroup child which contains the tab Views so you can use this code to change the font:

ViewGroupvg= (ViewGroup) indicator.getChildAt(0);
intvgChildCount= vg.getChildCount();
for (intj=0; j < vgChildCount; j++) {
    ViewvgChild= vg.getChildAt(j);
    if (vgChild instanceof TextView) {
        ((TextView) vgChild).setTypeface(YOUR_FONT);
    }
}

Solution 3:

So I've kind of brute forced my way into making it work, using a library called Oak (Where the TextViewWithFont is), and passing the PagerIndicator into this method:

privatestaticvoidchangeFonts(ViewGroup root, Activity a, String typeface) {
    Typefacetf= TextViewWithFont.getStaticTypeFace(a, typeface);

    for (inti=0; i < root.getChildCount(); i++) {
        Viewv= root.getChildAt(i);
        if (v instanceof TextView) {
            ((TextView) v).setTypeface(tf);
        } elseif (v instanceof ViewGroup) {
            changeFonts((ViewGroup) v, a, typeface);
        }
    }
}
publicstaticvoidsetTabPageIndicatorFont(ViewGroup root, Activity a) {
    changeFonts(root, a, "DINCondBold.otf");
}

Oak if anyones interested: http://willowtreeapps.github.com/OAK/

If anyone has a better way to go about it, I'd love to get a second opinion.

Post a Comment for "Tabpageindicator Text Font"