Question

I tried to set a custom font in a TextView.

Therefore I copied the otf file into /src/main/assets/fonts and added the following part to my onCreate within the Activity:

TextView tv = (TextView) findViewById(R.id.profil_fullname);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MavenProLight-100.otf");
    tv.setTypeface(tf);

But it does not work. I get no error and the app works, but the font is not the intended one. Do I have to add the asset folder to my build.gradle?!

EDIT1: I changed the fontname to MavenProLight.tff (I also converted it into a truetype font). Furthermore I moved it to src/main/assets and builded it again. Nothing changed so far and the Logcat does not contain any problem specific warnings or debug infos.

EDIT2: I found the problem! I used the design as layout within a Fragment. Therefore the onCreate of the Activity never gets called. Look at this page for more information: Set custom font for Android fragments

Was it helpful?

Solution

Please delete folder fonts sub folder of assets folder and also rename mavenProLight.otf instead of MavenProLight-100.otf. then try this.

        TextView tv = (TextView) findViewById(R.id.profil_fullname);
        Typeface tf = Typeface.createFromAsset(getAssets(), "mavenProLight.otf");
        tv.setTypeface(tf);

May be get works 100%. Best of luck!

OTHER TIPS

problem is with your path /src/main/assets/fonts . place the fonts in assets/fonts folder and it will wrk :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top