1

Following this procedure, I want to set the font of all text in the android app to a specific name. It is a non-english font, so I paste that in app\src\main\assets\fonts and wrote the following in styles.xml

    <item name="android:fontFamily">@fonts/my.ttf</item>
    <item name="fontFamily">@fonts/my.ttf</item>

However, after building and running the app inside the android studio (the emulator pops up), I see the default font and not the font I specified.

BTW, in that post, it uses @font. However, if I write that, I see an error that the font is not found. By changing @fonts, the compilation error is solved!

What is missing here?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mahmood
  • 23,197
  • 49
  • 147
  • 242

2 Answers2

0

Creating a font family

A font family is a set of font files along with its style and weight details. In Android, you can create a new font family as an XML resource and access it as a single unit, instead of referencing each style and weight as separate resources. By doing this, the system can select the correct font based on the text style you are trying to use.

To create a font family, perform the following steps in the Android Studio:

  1. Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
  2. Enter the file name, and then click OK. The new font resource XML opens in the editor.
  3. Enclose each font file, style, and weight attribute in the element. The following XML illustrates adding font-related attributes in the font resource XML:

      **<font-family 
            <font
                android:fontStyle="normal"
                android:fontWeight="400"
                android:font="@font/lobster_regular" />
            <font
                android:fontStyle="italic"
                android:fontWeight="400"
                android:font="@font/lobster_italic" />
        </font-family>**
    

Adding fonts to a TextView

To set a font for the TextView, do one of the following:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>

Check this out for complete documentation.

Hope this helps you.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
0

If you want to override the font for all the views you can use this approach:

public static Typeface overrideFont(Context context,
                                String defaultFontNameToOverride,
                                String customFontFileNameInAssets) {
    try {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
        final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
        defaultFontTypefaceField.setAccessible(true);
        defaultFontTypefaceField.set(null, customFontTypeface);
        return customFontTypeface;
    } catch (NoSuchFieldException e) {
        Logger.e(e);
        return null;
    } catch (IllegalAccessException e) {
        Logger.e(e);
        return null;
    }
}

And then in onCreate() method in your Application class just call this method like this:

TypefaceUtil.overrideFont(this,
            "SERIF",
            "fonts/CaviarDreams.ttf");

Do NOT forget to put your .ttf Font File in assets folder

Alexei Artsimovich
  • 1,074
  • 7
  • 15