2

I'm trying to load a custom font as follows:

private Paint customFont18; customFont18 = new Paint(); customFont18.setTextSize(18); Typeface fontFace = Typeface.createFromAsset(getAssets(), "FONT.TTF"); customFont18.setTypeface(fontFace); 

The getAssets fails, thows this:

-The method getAssets() is undefined for the type MyClass -assetManager cannot be resolved to a variable 

What is my problem? I've seen several examples like this but none works in my case. Thanks in advance.

3
  • no, it must be activity to work? Commented Mar 4, 2011 at 15:25
  • No, it doesn't have to be an activity in order to work. Where in the project is your FONT.TTF file located? Commented Mar 4, 2011 at 15:29
  • the font is located in the "assets" folder Commented Mar 4, 2011 at 15:30

3 Answers 3

11

getAssets() is a method of Context. If your class is not an activity, you'll need to pass a context into it and then call getAssets() on that.

public myClass(Context myContext) { Typeface typeface = Typeface.createFromAsset(myContext.getAssets(), "FONT.TTF"); ... } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try changing like this:

Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/FONT.TTF"); 

Comments

0

Use simple EasyFonts third party library to set variety of custom font to your TextView. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.

Simply:

TextView myTextView = (TextView)findViewById(R.id.myTextView); myTextView.setTypeface(EasyFonts.robotoThin(this)); 

This library also provides variety of font faces.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.