Getting an error in customizing the style of custom text in the application

I use a special font for TextView and EditText in an Android application. I made a separate class for edittext and textview fonts. I use the Calibri font for my text.

But after starting my application, the following error is displayed in all files: -

com.pack.demo.MyFontedTextView failed to create an instance where MyFontedTextView is a class file and com.pack.demo.MyFontedEditText failed to create an instance.

In all my layouts, I get a 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 a font error Typeface = Typeface.createFromAsset (context.getAssets (), "calibri.otf"); in the file below the class.

Here is the code for my edittext font -

    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);
        }
    }

, -

  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);
    }
}
+4
1

, , .

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) {
        }
    }
}
+1

All Articles