Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: font in Android Studio

I am trying to set a font to a text view in android studio but for some reasons, I cannot access the font directory.

I get Unresolved Reference: font

What I did

- Created a new directory by right clicking res > new > Android Resource Directory > Directory name : font > Resource type : font

- Placed my font in that directory, so the directory reads - res/font/myfont.ttf

- In my fragment I tried to access the font like this

var typeFace: Typeface? = ResourcesCompat.getFont(requireContext(), R.font.myfont.ttf)

Error shows Unresolved Reference: font

I tried to rebuild project but still not working.

like image 885
Ibramazin Avatar asked Jun 09 '26 04:06

Ibramazin


1 Answers

Use:

typeFace: Typeface? = ResourcesCompat.getFont(requireContext(), R.font.myfont)

Do not add .ttfextension.

like image 85
Prajwal Waingankar Avatar answered Jun 10 '26 20:06

Prajwal Waingankar



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!