AppCompatEditText Custom Typeface doesn't work properly

208 views Asked by At

I am trying to create a new and Custom AppCompatEditText while extending from AppCompatEditText, when I'm changing the typeface in my class and use that class in my XML while making the view, the font will remain the same as default, can anyone please tell me what is the problem here?

public class MAppEditText extends AppCompatEditText {

public MAppEditText(@NonNull @NotNull Context context) {
    super(context);
    init();
}

public MAppEditText(@NonNull @NotNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MAppEditText(@NonNull @NotNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    Typeface tf;

    if (getTypeface().isBold()) {
        tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/yekan_bold.ttf");
    } else {
        tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/yekan_regular.ttf");
    }
    setTypeface(tf);
}

}

I also made a custom AppCompatTextView with this method and it works just fine, but with AppCompatEditText is different.

1

There are 1 answers

2
Alfonso Tesone On

Are you sure you used the same object in the xml too?

 <MAppEditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@null"/>