Question

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!

Was it helpful?

Solution

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");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top