2

In my previous projects, I have used Calligraphy library to set a font for a whole application. But it requires storing font file in assets which makes APK size bigger. Now I am wondering is it possible to set a downloadable font as default for a whole application.

I could set a downloadable font for only ONE TextView.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:fontFamily="@font/lato"
    android:text="@string/large_text" />

Yes, I know I could create MyTextView class and set downloadable font programmatically. But I do not think it is a good idea as text can be anywhere in EditText, Spinner item, Toast.

So my question is how to set a downloadable font for a whole application as default font?

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109

3 Answers3

1

To apply a font set in XML everywhere in your app, create a theme in your themes.xml and set android:fontFamily in it.

<style name="ApplicationTheme">
    <item name="android:fontFamily">@font/lato</item>
</style>

Set this theme to your application in the Manifest

<application
    android:name=".App"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/ApplicationTheme">
...
</application>

and as long as you are not using style inheriting from system styles like

<style name="CustomButton" parent="Base.Widget.AppCompat.Button.Borderless">

your font will get applied to all TextViews, EditTexts, Spinners, Toasts, etc.

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
0

A library to help with custom fonts and text sizes

The goal of this library is to allow your app to support multiple FontFamilies (e.g. lato, roboto, etc.) with their own styles (e.g. normal, bold, italic) in an easily-configurable way.

global_warming
  • 833
  • 1
  • 7
  • 11
0
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();
        }
    }
}


public final class Application extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
        FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
        FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf");
        FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");
    }
}
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35