user1025050
user1025050

Reputation:

Android:Error implementing typeface

I am making an app in which my class extends RelativeLayout and implements OnClickListener But my problem is that i am not able to set TypeFace in that its giving Exception

public class Viewnew extends RelativeLayout implements OnPageChangeListener {
Context mContext;

and when i use

Typeface tf=Typeface.createFromAsset(mContext.getAssets(), "fonts/trbuc.ttf");

I am getting exception at mContext and exception is as follows:

E/AndroidRuntime(15203): Caused by: java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(15203): at android.graphics.Typeface.<init>(Typeface.java:240)
E/AndroidRuntime(15203):at android.graphics.Typeface.createFromAsset(Typeface.java:214)

Upvotes: 1

Views: 992

Answers (4)

user1025050
user1025050

Reputation:

I used .otf in place of .ttf and it worked.Thanks everyone for help

Upvotes: 0

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Use Following Code for that.

// Font path
String fontPath = "fonts/trbuc.ttf";

// text view label
TextView txtGhost = (TextView) findViewById(R.id.mTxtViewCustomFont);

// Loading Font Face
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);

// Applying font
txtGhost.setTypeface(tf);

Upvotes: 1

Zaid Daghestani
Zaid Daghestani

Reputation: 8615

The context is obviously not the issue, as this is not a NullPointer.

The issue is the font file you are specifying. First, check that the path is correct. If it is, try loading a different font file and see if that works. Android is not able to load all fonts. It is picky. So if you can find a similar font, try that. I hope this ends up working for you.

Upvotes: 0

Vipul
Vipul

Reputation: 28093

Change to

Typeface tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/trbuc.ttf");

Upvotes: 1

Related Questions