4
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?

Drilon Berisha
  • 207
  • 1
  • 2
  • 14

3 Answers3

9

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.

jburns20
  • 3,147
  • 2
  • 26
  • 32
  • User Gábor suggests: use `if (!isInEditMode())` to make sure it doesn't run in Eclipse. – jburns20 Jul 24 '13 at 18:55
  • hi, i get a class cast exception when i try to link it with my xml view. – Adifyr Dec 23 '13 at 14:04
  • jburns20, this is very old and I am not sure if that's relevant but you can always use isInEditMode() check in the constructor to solve the "preview" issues – inteist Aug 24 '17 at 13:39
1

if it's a custom font put it in asset folder and change it like this.

public class WeeklyFragment extends Fragment{


PagerTitleStrip _Title;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.weekly_fragment, container, false);                   
    _Title = (PagerTitleStrip)rootView.findViewById(R.id.__Weekly_pager_title_strip);
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "your font.ttf"); 

    for (int counter = 0 ; counter<_Title.getChildCount(); counter++) {

        if (_Title.getChildAt(counter) instanceof TextView) {
            ((TextView)_Title.getChildAt(counter)).setTypeface(font);
            ((TextView)_Title.getChildAt(counter)).setTextSize(25);
        }
    }
Daniel
  • 3,322
  • 5
  • 30
  • 40
0

While I think ViewPager supports android:textAppearance, you can't set the typeface that way either.

You are probably best served copying the PagerTitleStrip code (in your SDK or available online), refactor your fork into your own package, and modify it to suit. Or, see if the ViewPagerIndicator library has one that you like that allows you to configure the typeface.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491