1)Implement the following 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)Integrate this code into your activity:
Typeface typeFaceDefault = Typeface.createFromAsset(getAssets(), "yourFont.ttf");
3)Include 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)Finally, utilize it like so:
setErrText(getString(R.string.your_err_txt), yourEdt);