Change font with text change notification in android

I know that we can change the font of the edit text using Typeface. But what about the errors we set for text editing? Take a look at the codes below:

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/ATaha.ttf");
private EditText mPasswordView;
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setTypeface(font);

With this code, I could only change the font for text editing, but when I fixed the error as follows:

mPasswordView.setError(getString(R.string.error_field_required));

The error notification font is the default font for Android and does not change using the face type. How to change this?

+4
source share
3 answers

You can use SpannableStringto install the font:

SpannableString s = new SpannableString(errorString);
s.setSpan(new TypefaceSpan(font), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mPasswordView.setError(s);

Span , Typeface:

public class TypefaceSpan extends MetricAffectingSpan {
    private Typeface mTypeface;
    public TypefaceSpan(Typeface typeface) {
        mTypeface = typeface;
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}
+5

Typeface , , HTML .

HTML-, TextView The CommonsBlog

face , , .

mPasswordView.setError(Html.fromHtml("<font face='MONOSPACE'>Error font is MONOSPACE</font>"));
+2

By setting a spannable line in the error message, or extend the EditText and override your own error drawing engine.

0
source

Source: https://habr.com/ru/post/1538923/


All Articles