Created
June 12, 2015 10:14
-
-
Save devrath/f5f6e07c0b8ac2a2600d to your computer and use it in GitHub Desktop.
How to set the font in application level for entire application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Yes with reflection. This works ([based on this answer](http://stackoverflow.com/a/16275257/360211)): | |
| import java.lang.reflect.Field; | |
| import android.content.Context; | |
| import android.graphics.Typeface; | |
| 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 in an [application](http://developer.android.com/reference/android/app/Application.html) class: | |
| public final class Application extends android.app.Application { | |
| @Override | |
| public void onCreate() { | |
| super.onCreate(); | |
| FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf"); | |
| FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf"); | |
| FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf"); | |
| FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf"); | |
| } | |
| } | |
| Or course if you are using the same font file, you can improve on this to load it just once. | |
| However I tend to just override one, say `"MONOSPACE"`, then set up a style to force that font typeface application wide: | |
| <resources> | |
| <style name="AppBaseTheme" parent="android:Theme.Light"> | |
| </style> | |
| <!-- Application theme. --> | |
| <style name="AppTheme" parent="AppBaseTheme"> | |
| <item name="android:typeface">monospace</item> | |
| </style> | |
| </resources> | |
| API 21 Android 5.0 | |
| == | |
| I've investigated the reports in the comments that it doesn't work and it appears to be incompatible with the theme `android:Theme.Material.Light`. | |
| If that theme is not important to you, use an older theme, e.g.: | |
| <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> | |
| <item name="android:typeface">monospace</item> | |
| </style> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resource: http://stackoverflow.com/a/16883281/1083093