1

I was wondering if it's possible to define an embedded typeface to use for all TextViews in a styles.xml file.

I understand it's not as embedded typefaces have to be set programmatically on each element.

The code I use to do so is the following:

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myfont.ttf");
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setTypeface(tf);

Now I'd like it if I was able to create the font from assets once and for all in an Activity, an Application singleton or anything persistent enough so I can reuse it in every View definition to set the font on every element I want.

Any advice will be greatly appreciated!

[EDIT] Any comment on Mao's solution's up/down sides will be very welcome!

Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
  • Quite similar question: http://stackoverflow.com/a/5185587/2636001. If you only have `TextView`s, see [this answer](http://stackoverflow.com/a/5185587/2636001). – dst Aug 21 '13 at 14:48
  • Nice "trick". I like Mao's answer though. Works oob. Any comment on how stable his solution will be is welcome! – Armel Larcier Aug 21 '13 at 14:58

1 Answers1

1

You can declare a public static variable (in your main activity) which contains your typeFace and instanciate it in the onCreate method.

public static Typeface tf;

@Override
protected void onCreate(Bundle savedInstanceState) {
    tf = Typeface.createFromAsset(this.getAssets(),"fonts/yourFont.ttf");
}
MrP
  • 1,408
  • 18
  • 23