How to add custom font to the text view in Android?

deepak - Sun Jan 22 2012 11:48:25 GMT-0500 (EST)

How to add custom font to the text view in Android?

How to add custom font to the text view in Android?

Search for: How to add custom font to the text view in Android?

deepak - Sun Jan 22 2012 11:52:37 GMT-0500 (EST)

Dealing with Inbuilt Fonts


This is easy. We can create a style with the font and
associate it with the text view.

<style name="CustomText">
   <item name="android:typeface">YourFontName</item>
</style>

 <TextView style="@style/CustomText" />

deepak - Sun Jan 22 2012 12:00:04 GMT-0500 (EST)

How do we add custom fonts?


1) Add the ttf file into the assets folder

2) Typeface font = Typeface.createFromAsset(getAssets(), "CustomFontName.ttf");
     txt.setTypeface(font) ;

We can use step two and associate any text views with the
type face.

deepak - Sun Jan 22 2012 12:08:57 GMT-0500 (EST)

What if i need all my text views to have the same custom font?

What if i need all my text views to have the same custom font?

deepak - Sun Jan 22 2012 12:11:17 GMT-0500 (EST)

How to make all text views use the same custom font?


Here is what we can do:

1) Create a Custom text view by extending the android text view

2) on initialization associate the new font to the text view

3) Use this custom text view when you need text views,

Sample Code


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();
}

public void init() {

    Typeface tf = Typeface.createFromAsset
(getContext().getAssets(), "font/chiller.ttf");
    setTypeface(tf ,1);

}
}

deepak - Sun Jan 22 2012 12:12:19 GMT-0500 (EST)

Any android control can use custom font by using above methods

Any android control can use custom font by using above methods