Custom font does not work in candy

Is it possible to use the geomanist font (custom) in android lollipop.

But it works in all other versions.

Here is my code

In class MyApplication

    FontsOverride.setDefaultFont(this, "DEFAULT", "geomanist-lightnew.ttf");
    FontsOverride.setDefaultFont(this, "MONOSPACE", "geomanist-lightnew.ttf");
    FontsOverride.setDefaultFont(this, "SERIF", "geomanist-lightnew.ttf");
    FontsOverride.setDefaultFont(this, "SANS_SERIF", "geomanist-lightnew.ttf");

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



}




}

thank

+4
source share
2 answers

I use this library and works great on all devices

Step -

1) Add this line to your gradle compile 'com.github.balrampandey19:FontOnText:0.0.1'

2) Add your font to the asset folder, like this one

3) Then replace your view in xml with

<com.balram.library.FotTextView
                    android:id="@+id/vno_tv"
                    .
                    .
                    android:textSize="14sp"
                    app:font="regular.ttf" />

this line is important for setting the special font you want app:font="regular.ttf"

You can do the same for Buttons Edittext

OR

If you want to use the same "Font" through the entire application, you can follow this here>

+4
dependencies {
            compile 'uk.co.chrisjenx:calligraphy:2.2.0'
        }
+1

Source: https://habr.com/ru/post/1678530/


All Articles