How to write style to error text of EditText in android?

Solution 1:

The solution is at the end and here is the screenshot:

Styled error


Some Explanation

You might be able to set the textcolor using the following line

yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));

However, this might not be guaranteed.


According to the source code, this Popup that shows is of type ErrorPopup which is an internal class inside TextView. The content of this Popup is a single TextView inflated from com.android.internal.R.layout.textview_hint

final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
      null);

The background of this Popup depends on whether it should be placed above the anchor:

if (above) {
    mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error_above);
} else {
    mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error);
}

Since all the android resources used to create the popup are internal and ultimately hard-coded, your best shot would be to create your own error popup. This would be very easy and you wouldn't really be interfering with the normal EditText because the default popup is merely used to show the error, and, thus, creating your own would be fine.


SOLUTION

I have created it here: WidgyWidgets

Solution 2:

I don't think you can customize its style that way since the error popup uses an internal style:

mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
                    com.android.internal.R.styleable.Theme_errorMessageBackground);
mView.setBackgroundResource(mPopupInlineErrorBackgroundId);

However, you can set a Spanned and a custom error icon using the overloaded setError(CharSequence, Drawable).

You can easily create a Spanned from HTML using fromHtml().

However, you still won't be able to set the popup background image :-(

Solution 3:

Please add it at the time of form validation if edit text field is blank.

            int ecolor = R.color.black; // whatever color you want
        String estring = "Please enter a valid email address";
        ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
        SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
        ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);

        edtEmail.requestFocus();
        edtEmail.setError(ssbuilder); 

when you write in edit text, error sign automatic goes off

Thanks Sachin