Using a custom typeface in Android Using a custom typeface in Android xml xml

Using a custom typeface in Android


Yes It is possible.

You have to create a custom view which extends text view.

In attrs.xml in values folder:

<resources>    <declare-styleable name="MyTextView">        <attr name="first_name" format="string"/>        <attr name="last_name" format="string"/>        <attr name="ttf_name" format="string"/>    </declare-styleable></resources>

In main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:lht="http://schemas.android.com/apk/res/com.lht"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView  android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:text="Hello"/>    <com.lht.ui.MyTextView          android:id="@+id/MyTextView"        android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:text="Hello friends"        lht:ttf_name="ITCBLKAD.TTF"        />   </LinearLayout>

In MyTextView.java:

package com.lht.ui;import android.content.Context;import android.graphics.Typeface;import android.util.AttributeSet;import android.util.Log;import android.widget.TextView;public class MyTextView extends TextView {    Context context;    String ttfName;    String TAG = getClass().getName();    public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);        this.context = context;        for (int i = 0; i < attrs.getAttributeCount(); i++) {            Log.i(TAG, attrs.getAttributeName(i));            /*             * Read value of custom attributes             */            this.ttfName = attrs.getAttributeValue(                    "http://schemas.android.com/apk/res/com.lht", "ttf_name");            Log.i(TAG, "firstText " + firstText);            // Log.i(TAG, "lastText "+ lastText);            init();        }    }    private void init() {        Typeface font = Typeface.createFromAsset(context.getAssets(), ttfName);        setTypeface(font);    }    @Override    public void setTypeface(Typeface tf) {        // TODO Auto-generated method stub        super.setTypeface(tf);    }}


Is there a way to do this from the XML?

No, sorry. You can only specify the built-in typefaces through XML.

Is there a way to do it from code in one place, to say that the whole application and all the components should use the custom typeface instead of the default one?

Not that I am aware of.

There are a variety of options for these nowadays:

  • Font resources and backports in the Android SDK, if you are using appcompat

  • Third-party libraries for those not using appcompat, though not all will support defining the font in layout resources


I did this in a more "brute force" way that doesn't require changes to the layout xml or Activities.

Tested on Android version 2.1 through 4.4. Run this at app startup, in your Application class:

private void setDefaultFont() {    try {        final Typeface bold = Typeface.createFromAsset(getAssets(), DEFAULT_BOLD_FONT_FILENAME);        final Typeface italic = Typeface.createFromAsset(getAssets(), DEFAULT_ITALIC_FONT_FILENAME);        final Typeface boldItalic = Typeface.createFromAsset(getAssets(), DEFAULT_BOLD_ITALIC_FONT_FILENAME);        final Typeface regular = Typeface.createFromAsset(getAssets(),DEFAULT_NORMAL_FONT_FILENAME);        Field DEFAULT = Typeface.class.getDeclaredField("DEFAULT");        DEFAULT.setAccessible(true);        DEFAULT.set(null, regular);        Field DEFAULT_BOLD = Typeface.class.getDeclaredField("DEFAULT_BOLD");        DEFAULT_BOLD.setAccessible(true);        DEFAULT_BOLD.set(null, bold);        Field sDefaults = Typeface.class.getDeclaredField("sDefaults");        sDefaults.setAccessible(true);        sDefaults.set(null, new Typeface[]{                regular, bold, italic, boldItalic        });    } catch (NoSuchFieldException e) {        logFontError(e);    } catch (IllegalAccessException e) {        logFontError(e);    } catch (Throwable e) {        //cannot crash app if there is a failure with overriding the default font!        logFontError(e);    }}

For a more complete example, see http://github.com/perchrh/FontOverrideExample