Using Custom Fonts in Android
June 23rd, 2010
Using a .ttf or .otf font in Android is fairly straightforward. Firstly create ‘assets/fonts/’ and put the files in there. Now using Typeface.createFromAsset we can create the typeface.
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/CustomFont.ttf");
To set TextView to use this typeface we use the setTypeface function.
TextView tv = (TextView) findViewById(R.id.texty); tv.setTypeface(tf);
You can, of course, use this in your custom views with Paint.setTypeface.
Words of Warning
In general I wouldn’t recommend using fonts other than Droid font family apart from in games. That is because Ascender Corp specifically developed the Droid font family for optimal quality and readability on small screen devices. In an application the font is typically a form in which to present some information rather than containing information itself. For example in a racing you might use a font that is elongated with spoilers resembling a car but you probably would not use such a font for an article about cars.
Limitations
As far as I’m aware it is not possible to reference a font in XML. Making something like this impossible:
<TextView android:id = ”@+id/texty” android:typeface = "@font/CustomFont" />
I think that the Android team may have purposefully excluded a mechanism for referencing custom typefaces in XML to deter people from using an insane custom font that is difficult to read and potentially quite large.
May 17th, 2011 at 6:33 am
how to set custom font to customized list in android?
please replay….
on sachin.worldnet@gmail.com
[Reply]
hejp Reply:
May 20th, 2011 at 7:48 pm
I guess you’d have to set the typeface with setTypeface in the getView function of the Adapter that your list is using.
[Reply]
chanakya Reply:
August 6th, 2011 at 12:58 pm
but its not working in the case of list can u show me some example that would be help full for me..
thanks & regards
chanakya V
[Reply]
April 17th, 2012 at 9:23 am
i really don’t have knowledge about android programming can any one help me to guide in which IDE i can use this code
[Reply]