Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom font for error message of TextInputLayout in android

I'm trying to utilize TextInput Layout for Floating Labels available in android material design, where i'm showing error using app:errorTextAppearance flag. For this errorTextAppearance I'm unable to apply custom font.

I can change color and other things but not able to apply custom font.

I'm trying match font style of "Please select question" with "Please enter answer" P.S "Please select question" was a plain text view and i was able to change font easily.

I can't post image but below link has it.

Input Text Layout

Could you please help me with this issue.

like image 216
Anjali Avatar asked Aug 30 '16 00:08

Anjali


Video Answer


1 Answers

As mentioned here:

You can use a SpannableString to set the font:

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

A custom Span class that has a specific Typeface set:

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);
        }
    }
like image 168
Adib Faramarzi Avatar answered Oct 06 '22 18:10

Adib Faramarzi