How to add external fonts to android application

AndroidAndroid Fonts

Android Problem Overview


I was looking for some stylish fonts for my android application. but the problem is how can i make my android application supportable for external fonts.

Thank you.

Android Solutions


Solution 1 - Android

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).

Solution 2 - Android

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*/);
       }
   }
}

Solution 3 - Android

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

Solution 4 - Android

To implement you need use Typeface go through with sample below

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
for (View view : allViews)
{
   if (view instanceof TextView) 
   {
      TextView textView = (TextView) view;
      textView.setTypeface(typeface);
      }
   }
}

Solution 5 - Android

> The easiest way to accomplish this is to package the desired font(s) > with your application. To do this, simply create an assets/ folder in > the project root, and put your fonts (in TrueType, or TTF, form) in > the assets. You might, for example, create assets/fonts/ and put your > TTF files in there. > > Then, you need to tell your widgets to use that font. Unfortunately, > you can no longer use layout XML for this, since the XML does not know > about any fonts you may have tucked away as an application asset. > Instead, you need to make the change in Java code, by calling > Typeface.createFromAsset(getAssets(), “fonts/HandmadeTypewriter.ttf”), > then taking the created Typeface object and passing it to your > TextView via setTypeface().

For more reference here is the tutorial where I got this:

http://www.androidguys.com/2008/08/18/fun-with-fonts/

Solution 6 - Android

I recommend this approach it very nice with adding name of custom font in typeface to styles.xml and putting your set of fonts into assets folder.

Solution 7 - Android

One more point in addition to the above answers. When using a font inside a fragment, the typeface instantiation should be done in the onAttach method ( override ) as given below:

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
}

Reason:
There is a short span of time before a fragment is attached to an activity. If CreateFromAsset method is called before attaching fragment to an activity an error occurs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbHaRaThView Question on Stackoverflow
Solution 1 - AndroidZelimirView Answer on Stackoverflow
Solution 2 - AndroidJabbir BashaView Answer on Stackoverflow
Solution 3 - AndroidJanaView Answer on Stackoverflow
Solution 4 - AndroidJabbir BashaView Answer on Stackoverflow
Solution 5 - AndroidbHaRaThView Answer on Stackoverflow
Solution 6 - Androidar-gView Answer on Stackoverflow
Solution 7 - AndroidAkil AdeshwarView Answer on Stackoverflow