0

I am working on an android app and is clueless about how I can change the default font on android. I don't want to root the system in order to change the font. The font must be changed throughout the system.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • you might be searching for this : http://stackoverflow.com/questions/2711858/is-it-possible-to-set-a-custom-font-for-entire-of-application – Astyan Mar 03 '17 at 15:38

3 Answers3

1

The only problem with setTypeface() is that, if you want it app wide, you have to utilize it with every view. This either means tons of redundant code or writing custom view classes that set the font on inflation. OR you can use these two nifty little methods within a helper class to set your applications fonts via reflection:

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();
    }
}

Then to set a font do something like MyHelperClass.setDefaultFont(this, "DEFAULT", "fonts/FONT_NAME.ttf");

That will set your default font for your app to your FONT_NAME.ttf font file.

zgc7009
  • 3,371
  • 5
  • 22
  • 34
0

you can put your custom font in assets/fonts

it neds to end with .ttf extension and than

Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
        "fonts/yourFont.ttf");
    setTypeface(tf);
Kiloreux
  • 2,220
  • 1
  • 17
  • 24
0

Create assets/fonts

enter image description here

Example code:

  number_text = (TextView)findViewById(R.id.hour_progress_number);
  number_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));

  minute_text = (TextView)findViewById(R.id.minute_progress_number);
  minute_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81