3

This is a follow up for this question:

How to set default font family for entire Android app

I got some custom fonts which I added to assets/fonts folder. Now I want to use a font in that library to be the default one for the entire application.

The accepted answer for the linked question above suggests using a style which is what I want, but with the attribute "android:fontFamily" which was only added as of API 16. Is there a way to use a style for older APIs as well?

Community
  • 1
  • 1
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

5 Answers5

1

If you want a default font for your android App you can use:

Calligraphy Library

and you need to initialize it from your application class once you do that the custom font will be applicable for all the activities of the app.

Just follow the lib wiki to setup it.

Oussaki
  • 1,449
  • 2
  • 21
  • 30
  • read first react after the man told that he need "android:fontFamily" – Vishal Patel Apr 24 '17 at 11:59
  • 1
    I actually looked into that as well, but I got another problem that due to a very old project, I'm using Eclipse so I need that library for Eclipse as well – CodeMonkey Apr 24 '17 at 12:26
1

I tried searching for a solution and found out that you cannot add a custom font as the default font for lower versions that is less than 16.
It works for in built fonts though. Here is a possible solution

Community
  • 1
  • 1
Dishonered
  • 8,449
  • 9
  • 37
  • 50
1

For "android:fontFamily"
- only accepted default fonts provide by your OS version LINK.
- after Android "O" you can use it with your resources folder LINK

So,

If You have existing project best and quicker way to set font in your app is Calligraphy Library suggest by @Oussaki.

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Roboto-Bold.ttf"/>

it reqire only fontPath="fonts/Roboto-Bold.ttf" after integrate it

And also available for Eclipse .Aar file

Edit

do not forget to put this line at your every root layout. LINK

tools:ignore="MissingPrefix"

Happy Coding :)

jbarat
  • 2,382
  • 21
  • 23
Vishal Patel
  • 2,931
  • 3
  • 24
  • 60
0

add font ttf file in your assets resource

try this below class custom text view and add custom class in your xml file

public class CustomTextView extends TextView {


    public CustomTextView(Context context) {
        super(context);
        this.setTypeface(getTypeFace(context));
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(getTypeFace(context));
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setTypeface(getTypeFace(context));
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.setTypeface(getTypeFace(context));
    }


    public Typeface getTypeFace(Context context) {
        Typeface font = Typeface.createFromAsset(context.getAssets(), "your_filename.ttf");
        return font;
    }
}

In xml file like :

<package_name.CustomTextView
        android:id="@+id/tv_downloading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/icon_close"
        android:textSize="100dp"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:background="@color/col_white"
        android:textColor="@color/colorPrimaryDark"
        android:visibility="visible" />
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
0

What I eventually did was to first add this class:

public class TypefaceUtil 
{

/**
 * Using reflection to override default typeface
 * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
 *
 * @param context                    to work with assets
 * @param defaultFontNameToOverride  for example "monospace"
 * @param customFontFileNameInAssets file name of the font from assets
 */
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) 
{
    final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
    {
        Map<String, Typeface> newMap = new HashMap<String, Typeface>();
        newMap.put("serif", customFontTypeface);
        try 
        {
            final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
            staticField.setAccessible(true);
            staticField.set(null, newMap);
        } 
        catch (NoSuchFieldException e) 
        {
            e.printStackTrace();
        } 
        catch (IllegalAccessException e) 
        {
            e.printStackTrace();
        }
    } 
    else 
    {
        try 
        {
            final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
            defaultFontTypefaceField.setAccessible(true);
            defaultFontTypefaceField.set(null, customFontTypeface);
        } catch (Exception e) 
        {
            Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
        }
    }
}

}

Then I just added this line in the Application class:

@Override
public void onCreate()
{
    super.onCreate();
    TypefaceUtil.overrideFont(getApplicationContext(), "serif", "fonts/Ubuntu-Regular.ttf");
}

The fonts directory which holds the fonts themselves is inside the assets directory which is inside src\main (src\main\assets\fonts)

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203