1

I have written this code:

Typeface tf = Typeface.createFromAsset(getAssets(), "lato.woff2");
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setTypeface(tf);

But it crashes because:

"Caused by: java.lang.RuntimeException: Font asset not found lato.woff2"

I put the font file, lato.woff2, in these folders:

src/main/assets/font/
src/main/assets/

So, what am I doing wrong?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
xRobot
  • 25,579
  • 69
  • 184
  • 304
  • 3
    android support only .ttf file for fonts family and put your .ttf file in assets folder only – Nitesh Pareek Oct 26 '16 at 10:48
  • Andoid supports only TTF (or) OTF format – Naveen Kumar M Oct 26 '16 at 10:52
  • @NiteshPareek Done, but now I get this error: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference – xRobot Oct 26 '16 at 10:59
  • 1
    check this [LINK](http://stackoverflow.com/questions/30549351/nullpointerexception-with-custom-font-from-assets) its solve your error – Nitesh Pareek Oct 26 '16 at 11:02
  • @NiteshPareek Thanks but I have just notices that the textview is in another .xml layout. How can I do ? – xRobot Oct 26 '16 at 11:44

4 Answers4

0

getAssets() from getApplicationContext().

Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(), "lato.woff2");
Piyush
  • 18,895
  • 5
  • 32
  • 63
udayatom
  • 126
  • 1
  • 7
0

If you add your font file under assests path :

 Typeface mTypeface == Typeface.createFromAsset(getApplicationContext().getAssets(), "lato.woff2");

if you add your font under assests/fonts/ folder path :

Typeface mTypeface == Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/lato.woff2");
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
0

You are able to not to reinvent the wheel, if you would use Calligraphy library. All you need to do is override onCreate() method in your application class like this:

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
                            .setFontAttrId(R.attr.fontPath)
                            .build()

And then inject into context (it will work for whole app, if you'll declare it in the BaseActivity of your app):

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

Now you can use custom fonts in your UI elements like this:

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Roboto-Bold.ttf"/>

Don't forget to specify application class in your manifest. Good luck!

0

If you have the tff files in /src/main/assets/fonts you can also create a custom TextView by overriding the constructor:

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;


public class OpenSansTextView extends TextView {
    public OpenSansTextView(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        this.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf"));
    }
}

and then in your xml layout use:

 <YOUR-PACKAGE-NAME.OpenSansTextView
            android:id="@+id/some_id"
            android:text="@string/my_string"
            android:textSize="16sp" />
isuPatches
  • 6,384
  • 2
  • 22
  • 30