3

I want to change the font-family of the EditText setError().

I've tried this:

editText.setError(Html.fromHtml("<span style=\"font-family: sans-serif !important\">hello</span>"));

I want to change the font family for error message in a Samsung mobile when the user has selected another font.

Any idea would be appreciated.

Thanks in advance

sastresa
  • 75
  • 8

2 Answers2

5

you can try this:

Typeface typeFaceDefault = Typeface.createFromAsset(getAssets(), "BYekan.ttf");

TextInputLayout inputLayout = (TextInputLayout) view.findViewById(R.id.lyt_input); 

SpannableStringBuilder ssbuilder = new SpannableStringBuilder(getString(R.string.err_wrong_input));
ssbuilder.setSpan(new CustomTypefaceSpan("", ActivityMain.typeFaceDefault), 0, ssbuilder.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);    
inputLayout.setError(ssbuilder);

then you have also included this file in your project:

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}
Parissa Kalaee
  • 606
  • 1
  • 9
  • 17
0

1)Add this class :

import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {
public static final Parcelable.Creator<CustomTypefaceSpan> CREATOR = new Parcelable.Creator<CustomTypefaceSpan>() {

    public CustomTypefaceSpan createFromParcel(Parcel in) {
        return new CustomTypefaceSpan(in);
    }

    public CustomTypefaceSpan[] newArray(int size) {
        return new CustomTypefaceSpan[size];
    }
};
private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

private CustomTypefaceSpan(Parcel in) {
    super(in.readString());
    newType = Typeface.createFromFile(in.readString());
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

}

2)use this code in you activity:

Typeface typeFaceDefault = Typeface.createFromAsset(getAssets(), "yourFont.ttf");

3)add this method in your activity:

   public void setErrText(CharSequence err,EditText view){
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(err);
    ssbuilder.setSpan(new CustomTypefaceSpan("", typeFaceDefault), 0, ssbuilder.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    view.setError(ssbuilder);
}

4)and the finally use it like this:

setErrText(getString(R.string.your_err_txt),yourEdt);