Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
cannot resolve method 'getFont(?)'

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); textView.setTypeface(typeface);

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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use this method from support library 26.

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

Source: https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html

about 4 years ago · Santiago Trujillo Report

0

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"
    xmlns:app="http://schemas.android.com/apk/res-auto">
        <font android:fontStyle="normal" android:fontWeight="400" 
            android:font="@font/myfont-Regular"
            app:fontStyle="normal" app:fontWeight="400"
            app:font="@font/myfont-Regular"/>
        <font android:fontStyle="italic" android:fontWeight="400"
            android:font="@font/myfont-Italic"
            app:fontStyle="italic" app:fontWeight="400"
            app:font="@font/myfont-Italic" />
</font-family>

Cheers!

about 4 years ago · Santiago Trujillo Report

0

Try something like this

Typeface yourFont = Typeface.createFromAsset(getAssets(), "fonts/yourFont.ttf");
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!