Reputation: 3761
Thanks to this post, I know you can add fonts in an activity and can use it within that class. Using:
TextView hindiTextView=(TextView)findViewById(R.id.txtbx);
Typeface hindiFont=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf");
mytextView.setTypeface(hindiFont);
But what if I want to add a font in a single place and use it in the entire applicaiton? (Like in manifest or some property file)
Upvotes: 0
Views: 1091
Reputation: 23638
Actually there is not way to implement common custom font for whole application. Checkout HERE Which is to say: no, you can't set a font for the entire application.
But still if you want to try out ,try as below which is referenced HERE:
Common fonts class:
public final class FontsOverride {
public static void setDefaultFont(Context context,
String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}
protected static void replaceFont(String staticTypefaceFieldName,
final Typeface newTypeface) {
try {
final Field StaticField = Typeface.class
.getDeclaredField(staticTypefaceFieldName);
StaticField.setAccessible(true);
StaticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
You then need to overload the few default fonts, for example:
FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset3.ttf");
Or course if you are using the same font file, you can improve on this to load it just once.
Upvotes: 1
Reputation: 2727
Create a custom class which extends TextView , add the font logic their . Instead of using android TextView in layout , use <com.mypackage.myTextView android:id="@+id/myid />
Upvotes: 0
Reputation: 3735
No, sorry. You can only add custom fonts using the code you've just written.
Upvotes: 0