Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom fonts on Android

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.

like image 299
karse23 Avatar asked Mar 13 '26 14:03

karse23


1 Answers

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");
    ...
}
like image 77
Blumer Avatar answered Mar 16 '26 04:03

Blumer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!