Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText.setError() yields invisible error text

I have a very simple EditText, as follows:

<EditText
     android:id="@+id/myedit"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:maxLength="32"/>

In some validation code, I use Android's EditText.setError() to show any validation errors. This works fine in OS 2.x but not on an OS 3.x device (Xoom) -- on the Xoom you can see the outline of the error popup but you cannot see the error text.

I'm guessing that the text is there, but it is invisible. How do I make it visible? I don't see an android:textColor that would relate to error text.

Also, if the text is indeed invisible, then any ideas why 2.x behaves differently to 3.x -- seems like this would cause backward-compatibility problems.

Thanks.

like image 785
jarmod Avatar asked Sep 01 '11 17:09

jarmod


2 Answers

It looks like you can work around this problem by calling EditText.setError() with a SpannableStringBuilder object rather than a String.

int ecolor = xxxx; // whatever color you want
String estring = "Input is incorrect";
ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
myedittext.setError(ssbuilder);
like image 177
jarmod Avatar answered Oct 23 '22 18:10

jarmod


For a more elegant solution try this one:

editText.setError(Html.fromHtml("<font color='red'>Error Message!</font>"));
like image 18
mostar Avatar answered Oct 23 '22 16:10

mostar