How to write style to error text of EditText in android?
I've seen the accepted answer but i don't like the proposed library
I think this is a bug in the Android framework and I filed a bug here: https://code.google.com/p/android/issues/detail?id=158590
EDIT: the android design library TextInputLayout widget can be used to obtain a better error handling on EditText.
See how it looks like here: https://www.youtube.com/watch?v=YnQHb0fNtF8
And how to implement here: http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168
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
The solution is at the end and here is the screenshot:
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
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 :-(