Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create typeface from font resource id

I have tried using Typeface.createFromAsset(getAssets(),"font/myfont.ttf"));

I know font directory should be in assets but in my case, I have an existing font resource directory so I thought asset manager will read from font resource directory but I was wrong. I am creating Typeface to set custom font for collapsingToolbarLayout.

I found this answer but it requires me to keep font in assets

like image 848
sziraqui Avatar asked Feb 08 '18 14:02

sziraqui


People also ask

How do I edit TTF files on Android?

To change font styles in GO Launcher, copy the TTF or OTF font files on your phone. Long press on the home screen and select GO Settings > Font > Select Font.

What font type does Android use?

Android O supports both . otf (OpenType) and . ttf (TrueType) font formats.


2 Answers

This worked

Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);

Found this in the tutorial link by @Eselfar

like image 199
sziraqui Avatar answered Oct 21 '22 18:10

sziraqui


If you want to create the TypeFace from a font stored in assets folder,

enter image description here

you don´t need to define the directory, just the name of the font:

Typeface typeface =  Typeface.createFromAsset(getAssets(),"app_font.ttf");

You can create the Typeface using the resource id of the font stored into the /font folder like this :

Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);

your font must be stored into the /font directory

enter image description here

like image 36
Jorgesys Avatar answered Oct 21 '22 18:10

Jorgesys