43

I Have a Custom View which draws text onto the Canvas. I want to change the font to a font stored in the assets folder.

I am using Android Studio so I created a folder src/main/assets and placed my ttf files in there.

Paint txt = new Paint()
Typeface font = Typeface.createFromAsset(getAssets(), "robotobold.ttf");
txt.setTypeface(font);

Problem is Android Studio doesn't recognize getAssets() inside my Custom View, however, it recognizes it inside my Activity. I have tried passing Typeface through from my Activity but when I do it it doesn't change the font.

Rahul
  • 3,293
  • 2
  • 31
  • 43
Caleb Bramwell
  • 1,332
  • 2
  • 12
  • 24
  • 1
    One suggestion, if you want to apply custom font from assets better create a custom class that extends TextView inside which you can set the typeface and use it in xml layouts so that, at runtime you don't need to apply the typeface for all TextViews. – Manjunath Aug 26 '13 at 05:28
  • Found a library usage, this contains multiple view like textview, editext, button and many more http://stackoverflow.com/a/42001474/4446392 – Chathura Jayanath Feb 03 '17 at 07:24

7 Answers7

70

You can use your View's getContext() method to get the current Context, then use it to get the assets:

Typeface font = Typeface.createFromAsset(getContext().getAssets(), "robotobold.ttf");
Phil
  • 35,852
  • 23
  • 123
  • 164
34

First of all, you have to keep your assets folder inside your project and not inside src/main.. And then, create a folder called fonts inside assets. then, put the specific font typeface ttf files inside it.You can use the font typeface in coding like:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/filename.ttf");
textview.setTypeface(type);
Kavin
  • 544
  • 3
  • 5
  • thanks I was trying "/fonts/ ..." and android.os just could not grab the font. Removing dash, made the android system able to recognize the font file. – sivi Feb 17 '15 at 09:07
  • Hey, what do you mean by your "inside your project"? The app can't find the fonts, I have them in "/src/main/assets/font" too. – Sercan Samet Savran Feb 21 '20 at 11:43
5

created a folder src/main/assets and placed font files in there.

in Activity

Typeface font = Typeface.createFromAsset(getAssets(),  "Mukta-Regular.ttf");
tv.setTypeface(font);

in Fragment

Typeface.createFromAsset(getActivity().getAssets(), "Mukta-Regular.ttf");
tv.setTypeface(font);
Sudhir Singh
  • 817
  • 11
  • 16
1

In order to reuse typefaces in my projects I create a class full of typeface methods, this way I dont have to create a new typeface every time.

I call the class FontClass and in the class there is a method for each typeface I need to use e.g:

public static Typeface getOpenSansRegular(Context c){
    return Typeface.createFromAsset(c.getAssets(), "OpenSans-Light.ttf");
}

Then I can use them like so:

TextView text = (TextView) findViewById(R.id.textview);
text.setTypeface(FontClass.getOpenSansRegular(getApplicationContext());
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
0

You have to place your assets folder inside "Project" folder and not in the "src" folder. You have placed your font in the "src/main/assets/robotobold.ttf" that's why it is not working. You need to place it like this "/assets/robotobold.ttf".

arshu
  • 11,805
  • 3
  • 23
  • 21
0
    Typeface robo = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf");
Rohit
  • 191
  • 2
  • 9
0

My mistake was adding the line

Typeface mFont = Typeface.createFromAsset(this.getAssets(), "abc.ttf");

before onCreate()

makkuzu
  • 485
  • 2
  • 4
  • 19