71

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.

bHaRaTh
  • 3,464
  • 4
  • 34
  • 32

7 Answers7

147

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

vidstige
  • 12,492
  • 9
  • 66
  • 110
Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • 6
    where is this assets folder? I am using android studio and I can find it in under src/ – intoTHEwild Aug 17 '13 at 20:01
  • 4
    It is not under src, assets folder should be under your main project folder (at the same level as: src, res, gen, bin...) Check: http://developer.android.com/tools/projects/index.html – Zelimir Aug 19 '13 at 09:07
  • 3
    @Zelimir isn't there a way to dis universally for the project? and not take each TextView and set it's typeface? – Libathos Feb 28 '14 at 10:56
  • @libathos - sounds reasonable, but I am not sure how to do that. Did not have time to investigate, have you? – Zelimir Mar 03 '14 at 12:18
  • @libathos well you can define a Typeface through xml but that actually requires more writing than having it in coding – Libathos Mar 04 '14 at 08:08
  • This way not. See questions above. – Zelimir Jun 07 '15 at 12:51
  • You can sub class the font in you project and use that font in your xml – Bubunyo Nyavor Jul 29 '15 at 07:14
  • Can a user import a font into the app at runtime or must all fonts live within the assets folder when the apk is built? – gonzobrains Jun 16 '16 at 22:19
12

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*/);
       }
   }
}
Shashanth
  • 4,995
  • 7
  • 41
  • 51
Jabbir Basha
  • 455
  • 6
  • 7
9

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);
Jana
  • 2,890
  • 5
  • 35
  • 45
7

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);
      }
   }
}
ugo
  • 2,705
  • 2
  • 30
  • 34
Jabbir Basha
  • 455
  • 6
  • 7
  • 1
    for `allViews` you have to loop through the layout using `getChildCount()` so you could just replace the foreach with a simple for loop. – Ahmed Hasn. Mar 09 '16 at 12:47
5

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/

CaptJak
  • 3,592
  • 1
  • 29
  • 50
bHaRaTh
  • 3,464
  • 4
  • 34
  • 32
2

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.

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
ar-g
  • 3,417
  • 2
  • 28
  • 39
  • 1
    Very good approach, I suggest to take it to an attention. And maybe is a good idea to copy main code and post here, if link won't be accessible in the future. – validcat Aug 25 '16 at 15:05
2

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.