Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize Typeface in a fragment

i have this line :

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Delius-Regular.ttf");

but the getAssets() parameter seems to bring some error, it is underlined with scary red lines, it says

The method getAssets() is undefined for the type ProfileFragment

ProfileFragment is the name of my class and it extends Fragment.

NOTE : The font style is also on the assets folder.

like image 220
CENT1PEDE Avatar asked Jun 01 '26 07:06

CENT1PEDE


1 Answers

You cannot get getAssets() from a fragment directly. You have to use getActivity().getAssets() instead of using only getAssets().


Use this

Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Delius-Regular.ttf");

instead of

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Delius-Regular.ttf");

Read more about Set custom font for Android fragments

like image 59
Pankaj Kumar Avatar answered Jun 02 '26 21:06

Pankaj Kumar



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!