Home » Android » java – cannot resolve method 'getFont(?)'

java – cannot resolve method 'getFont(?)'

Posted by: admin May 14, 2020 Leave a comment


As the documentation states:

Android O lets you bundle fonts as resources by adding the font file
in the res/font/ folder.

As a result:

You can retrieve fonts by using the getFont(int) method, where you
need to pass the resource identifier of the font that you want to
retrieve. This method returns a Typeface object. This encodes the
first of the weight or style variants of your font, if it is a font
family. You can then use the Typeface.create(typeface, style) method
to retrieve specific styles.

Note: The TextView already does this for you.

Typeface typeface = getResources().getFont(R.font.myfont);

Unfortunately when I am using the above code I get the following error:

cannot resolve method 'getFont(?)'

Previously I used to do the following:

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");

But now when I try to create the fonts folder inside assets folder, it automatically jumps inside res folder.

I am using android studio 2.3. Thanks in advance for any help.

How to&Answers:

Use this method from support library 26.

ResourcesCompat.getFont(context, R.font.your_font);



Try something like this

Typeface yourFont = Typeface.createFromAsset(getAssets(), "fonts/yourFont.ttf");


I haven’t use it yet however as far as I know you can target Android O device only if you update your Android Studio to 2.4. And you need to update your SDK to latest build as well. Because as doc says getFont() available in Android O.


Since it’s a new method being released in O, you have to target the developer preview as your build target to use it. For more info, check this link.


Since I was not able to create fonts folder directly from Android Studio, I opened the project folder using windows explorer and created the fonts inside app\src\main\assets\ folder and pasted my font file there.

As so many other guys suggested in the answers I had to use Android Studio 2.4 Canary to be able to use Android O new features. The documentation states:

Only Android Studio 2.4 includes support for all the new developer
features available with Android O. So you need to get the canary
version of Android Studio 2.4 to begin using the Android O SDK. But
you can still keep your stable version of Android Studio installed.


I know this might be late but here’s the documentation for using downloaded fonts with API versions before 26 (compatibility mode).

Basically, you have to define the font description as shown below (using both attributes – app and android) then use the ResourceCompat class to access your font programmatically:

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
        <font android:fontStyle="normal" android:fontWeight="400" 
            app:fontStyle="normal" app:fontWeight="400"
        <font android:fontStyle="italic" android:fontWeight="400"
            app:fontStyle="italic" app:fontWeight="400"
            app:font="@font/myfont-Italic" />



They way I did it was like this:

private TextView textView;

protected void onCreate(Bundle savedInstanceState) {

textView = (TextView) findViewById(R.id.textView);
Typeface mTextView = Typeface.createFromAsset(getAssets(),"fonts/your_font_style.ttf");

Hope this will help you.


The fonts folder should be under assets folder, so try to create folder there.