Can we achieve a font override for an entire application with BOLD / LIGHT fonts?
I tried the below code and it's changing the fonts. But I'm not able to achieve bold and light font styles.
public 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) {
if (Build.VERSION.SDK_INT >= 21) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
String fontStyle = staticTypefaceFieldName.toLowerCase().replace("_", "-");
newMap.put(fontStyle, newTypeface);
try {
final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
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();
}
}
}
}
And then add below code in application onCreate method..
FontsOverride.setDefaultFont(this, "SANS_SERIF", "Montserrat-Regular.ttf");
I tried with the
FontsOverride.setDefaultFont(this, "SANS_SERIF_LIGHT", "Montserrat-Light.otf");
but no luck. Is there any way we can do this?