How to programmatically access pre-loaded fonts

218 Views Asked by At

I have mentioned pre-loaded fonts details as meta data in Android Manifest(as per the section Pre-declaring fonts in the manifest of Downloadable Fonts). I am able to use pre-loaded font in .xml file by app:fontFamily="@font/aladin" (here, xmlns:app="http://schemas.android.com/apk/res-auto")

But whenever I try to access pre-loaded font programmatically, I am getting the below exception: Caused by: android.content.res.Resources$NotFoundException: Font resource ID #0x7f090000

Code Snippet to access pre-loaded font: textView.setTypeface(ResourcesCompat.getFont(this, R.font.aladin));

Any help or guidance will be well appreciated.

1

There are 1 best solutions below

1
Toby On

If you have stored the font in resources

TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);

Your error indicates that you got some reference wrong, so you should check your references