4

I am using a custom font for TextView and EditText in my android app. I have made a seperate class for fonted edittext and textview. I am using the Calibri font for my text.

But after running my app, it is showing following error in all files as -

com.pack.demo.MyFontedTextView failed to instantiate where MyFontedTextView is the class file and com.pack.demo.MyFontedEditText failed to instantiate.

In all my layouts, I am getting the runtime exception as -

  java.lang.RuntimeException: native typeface cannot be made
 at android.graphics.Typeface.<init>(Typeface.java:175)
 at android.graphics.Typeface.createFromAsset(Typeface.java:149)
 at com.pack.demo.MyFontedTextView.<init>(MyFontedTextView.java:22)

com.pack.demo.MyFontedTextView.(MyFontedTextView.java:22) shows error in Typeface font = Typeface.createFromAsset(context.getAssets(), "calibri.otf"); in below class file.

Here is the code for my fonted edittext -

    public class MyFontedEditText extends EditText {

        public MyFontedEditText(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            Typeface font = Typeface.createFromAsset(context.getAssets(),
                    "calibri.otf");
            this.setTypeface(font);
        }

        public MyFontedEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            Typeface font = Typeface.createFromAsset(context.getAssets(),
                    "calibri.otf");
            this.setTypeface(font);
        }

        public MyFontedEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            Typeface font = Typeface.createFromAsset(context.getAssets(),
                    "calibri.otf");
            this.setTypeface(font);
        }
    }

Similarly, my code for fonted textview -

  public class MyFontedTextView extends TextView {

    public MyFontedTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "calibri.otf");
        this.setTypeface(font);
        this.setTypeface(null, Typeface.BOLD);
    }

    public MyFontedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "calibri.otf");
        Log.d("1", "abc");
        this.setTypeface(font);
        this.setTypeface(null, Typeface.BOLD);
    }

    public MyFontedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "calibri.otf");
        this.setTypeface(font);
        this.setTypeface(null, Typeface.BOLD);
    }
}
Ashutosh
  • 125
  • 1
  • 11
  • did add your font in assets folder ? – Sishin Sep 01 '14 at 05:34
  • @Sishin..yes i have added but still its not working.. – Ashutosh Sep 01 '14 at 05:45
  • I think it is typo mistake. once check your .ttf file name to the name which you mentioned in class. – Sonali8890 Sep 01 '14 at 05:48
  • hope you don't have fonts folder inside assets? – Sishin Sep 01 '14 at 05:49
  • yes.. i have checked..you can mention either ttf or otf extension files. And I have tried both, the issue is the same. – Ashutosh Sep 01 '14 at 05:50
  • if you have fonts folder then make it like. Typeface font = Typeface.createFromAsset(getAssets(), "fonts/calibri.otf"); – Sishin Sep 01 '14 at 05:51
  • fonts folder is not neccessary therefore not mentioning in the code – Ashutosh Sep 01 '14 at 05:52
  • check this link http://stackoverflow.com/questions/7531856/issue-when-using-a-custom-font-native-typeface-cannot-be-made – Rohan Pawar Sep 01 '14 at 05:56
  • @Rohan - your link is telling how to add font through holder in a list view. But I want to add font to many files and in many places so I want to make a seperate file for it...how should I sort my problem. – Ashutosh Sep 01 '14 at 06:02
  • 1
    @Ashutosh i just wanted to tell you that check your font is not corrupted – Rohan Pawar Sep 01 '14 at 06:06
  • and also why are calling "setTypeface" twice you can call like this also this.setTypeface(font, Typeface.BOLD); – Rohan Pawar Sep 01 '14 at 06:08
  • Check : http://stackoverflow.com/questions/23536793/my-custom-font-not-setting-up-in-edit-text/23537041#23537041 – Haresh Chhelana Sep 01 '14 at 06:15
  • @RohanPawar - but that shouldn't be the error..but then also I have changed but what about the edittext. – Ashutosh Sep 01 '14 at 06:20
  • @HareshChhelana - The link which you have send shows one edittext and it is describing typeface for one..i have to use it in multiple class files and in multiple locations so can't mention typeface line for each edittext. – Ashutosh Sep 01 '14 at 06:23
  • where is `calibri.otf` located ? Tell us path relative to project root. Is it in `yourApp/assets` ? – Alexander Malakhov Sep 01 '14 at 07:36

1 Answers1

1

Try this way,hope this will help you to solve your problem.

public class MyFontedTextView extends TextView {

    private Context context;
    public MyFontedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public MyFontedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyFontedTextView(Context context) {
        super(context);
        this.context=context;
        init(context);
    }

    private void init(Context mContext) {
        try {
            Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "calibri.otf");
            setTypeface(tf,Typeface.BOLD);
        } catch (Throwable e) {
        }
    }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67