简体   繁体   中英

How to set a custom font for the entire application in Android

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.

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").

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM