How to add external fonts to android application How to add external fonts to android application android android

How to add external fonts to android application


You need to create fonts folder under assets folder in your project and put your TTF into it. Then in your Activity onCreate()

TextView myTextView=(TextView)findViewById(R.id.textBox);Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");myTextView.setTypeface(typeFace);

Please note that not all TTF will work. While I was experimenting, it worked just for a subset (on Windows the ones whose name is written in small caps).


You can use the custom TextView for whole app with custom font here is an example for that

public class MyTextView extends TextView {   Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);   Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(),  Constants.FONT_BOLD);   public MyTextView(Context context, AttributeSet attrs, int defStyle) {       super(context, attrs, defStyle);   }   public MyTextView(Context context, AttributeSet attrs) {       super(context, attrs);   }   public MyTextView(Context context) {       super(context);   }   public void setTypeface(Typeface tf, int style) {       if (style == Typeface.BOLD) {           super.setTypeface(boldTypeface/*, -1*/);       } else {           super.setTypeface(normalTypeface/*, -1*/);       }   }}


Create a folder named fonts in the assets folder and add the snippet from the below link.

Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");textview.setTypeface(tf);