[ACCEPTED]-Using custom font in android TextView using xml-fonts

Accepted answer
Score: 45

Short answer: No. Android doesn't have built-in 22 support for applying custom fonts to text 21 widgets through XML.

However, there's a workaround 20 that's not terribly difficult to implement.

First

You'll 19 need to define your own stylable. In your 18 /res/values folder, open/create the attrs.xml 17 file and add a declare-styleable object 16 like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FontText">
        <attr name="typefaceAsset" format="string"/>
    </declare-styleable>
</resources>

Second

Assuming you want to use this widget 15 often, you should set up a simple cache 14 for the loaded Typeface objects, since loading them 13 from memory on the fly can take time. Something 12 like:

public class FontManager {
    private static FontManager instance;

    private AssetManager mgr;

    private Map<String, Typeface> fonts;

    private FontManager(AssetManager _mgr) {
        mgr = _mgr;
        fonts = new HashMap<String, Typeface>();
    }

    public static void init(AssetManager mgr) {
        instance = new FontManager(mgr);
    }

    public static FontManager getInstance() {
        if (instance == null) {
            // App.getContext() is just one way to get a Context here
            // getContext() is just a method in an Application subclass
            // that returns the application context
            AssetManager assetManager = App.getContext().getAssets();
            init(assetManager);
        }
        return instance;
    }

    public Typeface getFont(String asset) {
        if (fonts.containsKey(asset))
            return fonts.get(asset);

        Typeface font = null;

        try {
            font = Typeface.createFromAsset(mgr, asset);
            fonts.put(asset, font);
        } catch (Exception e) {

        }

        if (font == null) {
            try {
                String fixedAsset = fixAssetFilename(asset);
                font = Typeface.createFromAsset(mgr, fixedAsset);
                fonts.put(asset, font);
                fonts.put(fixedAsset, font);
            } catch (Exception e) {

            }
        }

        return font;
    }

    private String fixAssetFilename(String asset) {
        // Empty font filename?
        // Just return it. We can't help.
        if (TextUtils.isEmpty(asset))
            return asset;

        // Make sure that the font ends in '.ttf' or '.ttc'
        if ((!asset.endsWith(".ttf")) && (!asset.endsWith(".ttc")))
            asset = String.format("%s.ttf", asset);

        return asset;
    }
}

This one will allow you to use .ttc 11 file extensions, but it's untested.

Third

Create 10 a new class that subclasses TextView. This particular 9 example takes into account the defined XML 8 typeface (bold, italic, etc.) and apply it to the 7 font (assuming you're using a .ttc file).

/**
 * TextView subclass which allows the user to define a truetype font file to use as the view's typeface.
 */
public class FontText extends TextView {
    public FontText(Context context) {
        this(context, null);
    }

    public FontText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FontText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        if (isInEditMode())
            return;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FontText);

        if (ta != null) {
            String fontAsset = ta.getString(R.styleable.FontText_typefaceAsset);

            if (!TextUtils.isEmpty(fontAsset)) {
                Typeface tf = FontManager.getInstance().getFont(fontAsset);
                int style = Typeface.NORMAL;
                float size = getTextSize();

                if (getTypeface() != null)
                    style = getTypeface().getStyle();

                if (tf != null)
                    setTypeface(tf, style);
                else
                    Log.d("FontText", String.format("Could not create a font from asset: %s", fontAsset));
            }
        }
    }
}

Finally

Replace 6 the instances of TextView in your XML with the fully 5 qualified class name. Declare your custom 4 namespace just like you would the Android 3 namespace. Note that the "typefaceAsset" should 2 point to a .ttf or .ttc file contained in 1 your /assets directory.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.FontText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a custom font text"
        custom:typefaceAsset="fonts/AvenirNext-Regular.ttf"/>
</RelativeLayout>
Score: 28

Here is example code that does this. I have 2 the font defined in a static final variable 1 and the font file is in the assets directory.

public class TextViewWithFont extends TextView {

    public TextViewWithFont(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(MainActivity.typeface);
    }

    public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setTypeface(MainActivity.typeface);
    }

    public TextViewWithFont(Context context) {
        super(context);
        this.setTypeface(MainActivity.typeface);
    }

}
Score: 13

Create your customed TextView belong to 4 the font you want to use. In this class, I 3 use a static mTypeface field to cache the 2 Typeface (for better performance)

public class HeliVnTextView extends TextView {

/*
 * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced.
 */
private static Typeface mTypeface;

public HeliVnTextView(final Context context) {
    this(context, null);
}

public HeliVnTextView(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}

public HeliVnTextView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);

     if (mTypeface == null) {
         mTypeface = Typeface.createFromAsset(context.getAssets(), "HelveticaiDesignVnLt.ttf");
     }
     setTypeface(mTypeface);
}

}

In xml 1 file:

<java.example.HeliVnTextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        ... />

In java class:

HeliVnTextView title = new HeliVnTextView(getActivity());
title.setText(issue.getName());
Score: 11

Activity implements LayoutInflater.Factory2 11 that provides callbacks on each created 10 View. It's possible to style the TextView 9 with custom font Family attribute, load 8 the typefaces on demand and call setTypeface 7 on instantiated text views automatically.

Unfortunately 6 due to the architectural relationship of 5 Inflater instances relative to Activities 4 and Windows the simplest approach to use 3 custom fonts in android is to cache loaded 2 fonts on the Application level.

The sample 1 code base is here:

https://github.com/leok7v/android-textview-custom-fonts

  <style name="Baroque" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#F2BAD0</item>
    <item name="android:textSize">14pt</item>
    <item name="fontFamily">baroque_script</item>
  </style>

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:custom="http://schemas.android.com/apk/res/custom.fonts"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
  >
  <TextView
    style="@style/Baroque"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/sample_text"
  />

results in

enter image description here

Score: 7

Not a good idea to use custom fonts in xml 2 due to this fact that is, you have to do it programmatically 1 to avoid the memory leak!

Score: 7

UPDATE: https://github.com/chrisjenx/Calligraphy appears to be a superior solution 10 to this.


Maybe you can use reflection to inject/hack your font into the static list of available fonts when your application 9 is created? I am interested in feedback 8 from others if this is a really, really bad idea or if this is 7 a great solution — it seems it is going to be one of those 6 extremes...

I was able to inject my custom 5 typeface into the list of system typefaces 4 with my own font family name, then specifying 3 that custom font family name ("brush-script") as 2 the value of android:FontFamily on a standard TextView worked 1 on my LG G4 running Android 6.0.

public class MyApplication extends android.app.Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();

        Typeface font = Typeface.createFromAsset(this.getResources().getAssets(),"fonts/brush-script.ttf");
        injectTypeface("brush-script", font);
    }

    private boolean injectTypeface(String fontFamily, Typeface typeface)
    {
        try
        {
            Field field = Typeface.class.getDeclaredField("sSystemFontMap");
            field.setAccessible(true);
            Object fieldValue = field.get(null);
            Map<String, Typeface> map = (Map<String, Typeface>) fieldValue;
            map.put(fontFamily, typeface);
            return true;
        }
        catch (Exception e)
        {
            Log.e("Font-Injection", "Failed to inject typeface.", e);
        }
        return false;
    }
}

In my layout

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fancy Text"
    android:fontFamily="brush-script"/>
Score: 6

Create a fonts folder in assets and add 3 all your required font's there.

public class CustomTextView extends TextView {
    private static final String TAG = "TextView";

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        String customFont = a.getString(R.styleable.CustomTextView_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String fontName) {
        Typeface typeface = null;
        try {
            if(fontName == null){
                fontName = Constants.DEFAULT_FONT_NAME;
            }
            typeface = Typeface.createFromAsset(ctx.getAssets(), "fonts/" + fontName);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }
        setTypeface(typeface);
        return true;
    }
}

and add a 2 declarable in attrs.xml

<declare-styleable name="CustomTextView">
      <attr name="customFont" format="string"/>
</declare-styleable>

and then add your customFont 1 like

app:customFont="arial.ttf"
Score: 6

I know this is an old question, but i've 5 found a much easier solution.

First declare 4 your TextView in xml as usual. Put your 3 font (TTF or TTC) in the asset folder

app\src\main\assets\

Then 2 just set the typeface for your text view 1 in your onCreate method.

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);    

    TextView textView = findViewById(R.id.my_textView);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "fontName.ttf");
    textView.setTypeface(typeface);
}

Done.

Score: 2

The best solution is to use (finally) introduced 3 by Google a native custom font feature in 2 XML. But you have to target API 26. It supports 1 API 16+

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

Score: 0

instead of xmlns:custom="schemas.android.com/tools"; you 3 should use: xmlns:custom="schemas.android.com/apk/res-auto"; in 2 order to use styleable attributes. I made 1 this change and it is working now.

Score: 0

the latest update now that you can set the 2 font in XML without any other classes added 1 like:

android:fontFamily="@font_folder/font_file"

More Related questions