Skip to content

Instantly share code, notes, and snippets.

@lezorich
Created September 11, 2014 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lezorich/1a69f5c1608f4079d496 to your computer and use it in GitHub Desktop.
Save lezorich/1a69f5c1608f4079d496 to your computer and use it in GitHub Desktop.
To change the font everywhere in the application we can use reflection to override the default fonts, with no custom views or changes in layouts xml
import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.Typeface;
/**
* reference: http://stackoverflow.com/a/16883281/1373226
*/
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment