Reference URL : https://gist.github.com/artem-zinnatullin/7749076?signup=true
| public class MyApp extends Application { |
| TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf |
}
| <?xml version="1.0" encoding="utf-8"?> |
| <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light"> |
| <!-- you should set typeface which you want to override with TypefaceUtil --> |
| <item name="android:typeface">serif</item> |
</
resources>
| import android.content.Context; |
| import android.graphics.Typeface; |
| import java.lang.reflect.Field; |
| public class TypefaceUtil { |
| * Using reflection to override default typeface |
| * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN |
| * @param context to work with assets |
| * @param defaultFontNameToOverride for example "monospace" |
| * @param customFontFileNameInAssets file name of the font from assets |
| public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) { |
| final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets); |
| final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride); |
| defaultFontTypefaceField.setAccessible(true); |
| defaultFontTypefaceField.set(null, customFontTypeface); |
| Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride); |
}
No comments:
Post a Comment