Skip to content

Instantly share code, notes, and snippets.

@nisargjhaveri
Created July 13, 2016 12:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisargjhaveri/47acc83e66dcb347e05685ccfd3038e8 to your computer and use it in GitHub Desktop.
Save nisargjhaveri/47acc83e66dcb347e05685ccfd3038e8 to your computer and use it in GitHub Desktop.
Set fallback fonts globally in android app
package com.example.customfonts;
import android.app.Application;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Build;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class CustomFontApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Set Fonts
setDefaultFonts();
}
private void setDefaultFonts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return;
}
// Works with Android API 21 to 23
// Not sure after that as it uses hidden APIs that might get removed or changed in future
Typeface normal = getTypefaceFromAssets(getAssets(), new String[]{"fonts/NotoSansDevanagari-Regular.ttf", "fonts/NotoSansGujarati-Regular.ttf"});
Typeface bold = getTypefaceFromAssets(getAssets(), new String[]{"fonts/NotoSansDevanagari-Bold.ttf", "fonts/NotoSansGujarati-Bold.ttf"});
Typeface italic = getTypefaceFromAssets(getAssets(), new String[]{});
Typeface boldItalic = getTypefaceFromAssets(getAssets(), new String[]{});
try {
Method setDefaultTypeface = Typeface.class.getDeclaredMethod("setDefault", Typeface.class);
setDefaultTypeface.setAccessible(true);
setDefaultTypeface.invoke(null, normal);
Field defaultField = Typeface.class.getDeclaredField("DEFAULT");
defaultField.setAccessible(true);
defaultField.set(null, normal);
Field defaultBoldField = Typeface.class.getDeclaredField("DEFAULT_BOLD");
defaultBoldField.setAccessible(true);
defaultBoldField.set(null, bold);
Field sDefaults = Typeface.class.getDeclaredField("sDefaults");
sDefaults.setAccessible(true);
sDefaults.set(null, new Typeface[]{normal, bold, italic, boldItalic});
Field sDefaultTypeface = Typeface.class.getDeclaredField("sDefaultTypeface");
sDefaultTypeface.setAccessible(true);
sDefaultTypeface.set(null, normal);
Field sansSerifDefaultField = Typeface.class.getDeclaredField("SANS_SERIF");
sansSerifDefaultField.setAccessible(true);
sansSerifDefaultField.set(null, normal);
Map<String, Typeface> newMap = new HashMap<>();
newMap.put("sans-serif", normal);
final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private static Typeface getTypefaceFromAssets(AssetManager mgr, String[] fontFileList) {
Class FontFamily;
Method addFontFromAsset;
try {
FontFamily = Class.forName("android.graphics.FontFamily");
addFontFromAsset = FontFamily.getDeclaredMethod("addFontFromAsset", AssetManager.class, String.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (NoSuchMethodException e) {
e.printStackTrace();
return null;
}
Object families = Array.newInstance(FontFamily, fontFileList.length);
for (int i = 0; i < fontFileList.length; i++) {
try {
Object newFontFamily = FontFamily.newInstance();
addFontFromAsset.invoke(newFontFamily, mgr, fontFileList[i]);
Array.set(families, i, newFontFamily);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Typeface tf = null;
try {
Method createFromFamiliesWithDefault = Typeface.class.getDeclaredMethod("createFromFamiliesWithDefault", Class.forName("[Landroid.graphics.FontFamily;"));
tf = (Typeface) createFromFamiliesWithDefault.invoke(null, families);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return tf;
}
}
@dengkis
Copy link

dengkis commented Nov 15, 2018

its work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment