26

How can I use a custom font which was added in the asset folder in my xml? I know we can use setTypeface() method in java, but we have to do this everywhere where we use that TextView. So is there a better way?

buczek
  • 2,011
  • 7
  • 29
  • 40
Vins
  • 4,089
  • 2
  • 35
  • 50
  • I have updated my answer. Please remove -ve from that answer. – Shreyash Mahajan Jan 27 '12 at 10:42
  • Try this Tutorial [http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/](http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/) I think it helps you – Ajay Jan 27 '12 at 10:15
  • Hi please refer this post . a problem with same nature was discussed and answered over there. – Wajeeh Jan 27 '12 at 07:37

2 Answers2

58

The best way i found by googling is- Say if you want to use in TextView then we have to extend the Textview and have to set the font in that later we can use our customised Textview in our xml. I'll show the extended TextView below

package com.vins.test;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                               "your_font.ttf");
        setTypeface(tf);
    }

}

We calling init() to set font in each of the costructors. Later we have to use this in our main.xml as shown below.

<com.vins.test.MyTextView
    android:id="@+id/txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="This is a text view with the font u had set in MyTextView class "
    android:textSize="30dip"
    android:textColor="#ff0000"
   >

Update:

Be aware about the memory leak in pre-4.0 Android as mentioned by pandre.

Vins
  • 4,089
  • 2
  • 35
  • 50
  • @vins - what if the user states also `android:textStyle="bold"`? Will the text be indeed set to bold? – AlikElzin-kilaka Aug 02 '13 at 12:14
  • @kilaka Yes it should turn bold. – Vins Aug 06 '13 at 04:25
  • 1
    @vins - thanks. Does this mean that the ttf file includes all sub-types of the font - bold, italic, etc. and combinations of them? – AlikElzin-kilaka Aug 06 '13 at 08:06
  • 4
    Please be aware that this solution causes Memory Leaks in older Android versions. Avoid this by caching the created Typeface. More info: https://code.google.com/p/android/issues/detail?id=9904 – pandre Oct 29 '13 at 15:31
  • 1
    This method is working. But the inflating of this custom textview takes 200 ms whereas default textview takes only 2 ms. Beware of this approach :( – nizam.sp Jan 24 '15 at 20:06
  • Hey @nizam.sp do you have any reference for that ? – Antwan Aug 25 '15 at 21:53
  • @Tony : The time taken was because of the memory leak mentioned by pandre. – nizam.sp Aug 26 '15 at 06:34
  • for further help I have fixed this issue here http://stackoverflow.com/questions/4395309/android-want-to-set-custom-fonts-for-whole-application-not-runtime/32216644#32216644 – Antwan Aug 26 '15 at 06:56
  • I find it really annoying to have to swap out the default `TextView` everywhere to use custom fonts. Combined with the limiting set of fonts available in android, it makes developing good looking apps a lot of hard work. I've added a feature request to the android issue tracker. If you'd like to see this feature, you can star the issue here: https://code.google.com/p/android/issues/detail?id=187475&thanks=187475&ts=1443175244 – Sam Sep 25 '15 at 10:04
  • This works for me, but i have one problem. I have attribute "android:layout_width="fill_parent" in my TextView. And it does not work with com.test.myapplication.MyTextView it looks like "wrap_content" Can anyone help me? – Dmitry Lyalin Jul 04 '16 at 13:36
  • the answer seems outdated with the Android O now you can use fonts directly using the xml . https://developer.android.com/preview/features/working-with-fonts.html#fonts-in-xml – Azmat Karim Khan Aug 21 '17 at 18:32
2

Put your font file in asset\fonts\fontname

Define three textview in your xml file then, put this code in your activity class:

public class AndroidExternalFontsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Font path
        String fontPath = "fonts/DS-DIGIT.TTF";
        String fontPath1 = "fonts/Face Your Fears.ttf";
        String fontPath2 = "fonts/HelveticaNeue-Bold_0.otf";

        // text view label
        TextView txtGhost = (TextView) findViewById(R.id.ghost);
        TextView txtGhost1 = (TextView) findViewById(R.id.ghost1);
        TextView txtGhost2 = (TextView) findViewById(R.id.ghost2);

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
        Typeface tf1 = Typeface.createFromAsset(getAssets(), fontPath1);
        Typeface tf2 = Typeface.createFromAsset(getAssets(), fontPath2);

        // Applying font
        txtGhost.setTypeface(tf);
        txtGhost1.setTypeface(tf1);
        txtGhost2.setTypeface(tf2);
    }
}
OGHaza
  • 4,795
  • 7
  • 23
  • 29
Mitul Goti
  • 2,657
  • 1
  • 22
  • 19
  • 13
    Hey this is normal way to set the font, the question was for using custom font by default in XML. – Vins Aug 06 '13 at 05:46