Change font TextInputLayout errors?

Is it possible to change the text font TextInputLayout for EditText?

I could only change the color or size of the text using app: errorTextAppearance.

+2
source share
2 answers

You can use 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); } } 
+3
source

Another method, if you like it, you can set the error color or font as

 public static void setErrorTextColor(TextInputLayout textInputLayout, int color, Typeface font) { try { Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView"); fErrorView.setAccessible(true); TextView mErrorView = (TextView) fErrorView.get(textInputLayout); Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor"); fCurTextColor.setAccessible(true); fCurTextColor.set(mErrorView, color); mErrorView.setTypeface(font); } catch (Exception e) { e.printStackTrace(); } } 
0
source

All Articles