1

Ok so I have research this and found some code relating to the topic but it doesn't seem to work when I try. I have been individually had to change the font for each text view and it's driving me insane. What I have done so far is create a class that will overide the font:

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

And then each time I wish to overide the font in each class I tried to implemnt this:

   FontsOverride.setDefaultFont(this, "DEFAULT", "ComicRelief.ttf");

There has to be simple way to do this I've been trying for hours and just can't get my head around it.

Eoin Ó Cribín
  • 155
  • 1
  • 3
  • 14
  • Possible duplicate of [Android - Using Custom Font](http://stackoverflow.com/questions/3651086/android-using-custom-font) – Ajit Kumar Dubey Nov 28 '15 at 11:43
  • Make use of style and use the style across the application – Shriram Nov 28 '15 at 11:45
  • yes as told by shriram you can use a style to set the font... and apply this style to all that components you want to change font – sud Nov 28 '15 at 11:52
  • Is there no simple way to override the font to avoid this as there will be a lot of styles and I will surely forget to override it each time – Eoin Ó Cribín Nov 28 '15 at 11:58

2 Answers2

0

Just create a custom TextView class:

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        load();
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        load();
    }

    public CustomTextView(Context context) {
        super(context);
        load();
    }

    public void load() {
        setTypeface(
            Typeface.createFromAsset(getContext().getAssets(), "pacifico.ttf"), 1
        );
    }

}

Apply the custom view for whichever TextView you wish:

<com.your.package.views.CustomTextView
    android:id="@+id/my_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Your TextView type is now declared and cast to CustomTextView:

CustomTextView myTextView = (CustomTextView) findViewById(R.id.my_text_view);

And remember to place the appropriate font into your assets folder (in this example, it's "pacifico.ttf").

mjp66
  • 4,214
  • 6
  • 26
  • 31
0

What you need to use is the Calligraphy library to set a custom font for the entire app.

In your custom Application class, init Calligraphy inside onCreate():

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/custom_font.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build());

Here, custom_font.ttf is the font you want to apply to the entire app, and it needs to reside inside assets/fonts folder.

Secondly, you need to override the attachBaseContext() method in your Activity classes as follows:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

Now, each and every text you see in your app has the font you set as default through Calligraphy.

Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73