5

I know we can change edit text font by using Typeface. But what about errors we set for edit text? Look at 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 edit text font but when I set error like this:

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

The error notification font is android default font and didn't change by using type face. How can I change that?

hamidfzm
  • 4,595
  • 8
  • 48
  • 80
  • 2
    I found a question that is similar to yours. [http://stackoverflow.com/questions/14413575/how-to-write-style-to-error-text-of-edittext-in-android][1] [1]: http://stackoverflow.com/questions/14413575/how-to-write-style-to-error-text-of-edittext-in-android – Andrew May 01 '14 at 21:54
  • @Andrew But the only thing discussed there is color and no one mention changing font and there is no conclusion there! – hamidfzm May 01 '14 at 21:58

3 Answers3

6

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);
    }
}
myanimal
  • 3,580
  • 26
  • 26
  • It's working for me on Android 4.4, but should work on all versions. Could you post your code for setting the error like this? – myanimal May 07 '14 at 17:51
  • Oh wait, I forgot I'm using a custom `TypefaceSpan` class. I'll update my answer now.. – myanimal May 07 '14 at 17:52
  • It worked very good but is there any way change `SpannableString` without using new and creating new objects? – hamidfzm May 08 '14 at 12:48
  • Not that I can think of, sorry. The overhead shouldn't be too much though, unless you have a form with 1000s of `EditTexts`, which would be crazy. – myanimal May 08 '14 at 13:41
2

Since you can't directly set a Typeface for error text, you can achieve it by setting an HTML string as a text inside it.

You can see HTML Tags supported by a TextView in The CommonsBlog

We have face attribute for font, which means you can change the font-family.

mPasswordView.setError(Html.fromHtml("<font face='MONOSPACE'>Error font is MONOSPACE</font>"));
canova
  • 3,965
  • 2
  • 22
  • 39
0

By setting spannable string in error message or extend EditText and overrite your own error draw mechanism.

Simpalm
  • 1
  • 5