Android EditText.setError() yields invisible error text

眉间皱痕 提交于 2019-11-28 06:53:10

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);

For a more elegant solution try this one:

editText.setError(Html.fromHtml("<font color='red'>Error Message!</font>"));

Use native parent theme for appropriate API level in your own customized style! "@android:style/theme.name".

Distinguish themes by Configuration qualifier:

value/style.xml - > parent="@android:style/Theme.Light"
value-v11/style.xml -> parent="@android:style/Theme.Holo.Light"
value-v14/style.xml -> parent="@android:style/Theme.DeviceDefault.Light"

http://code.google.com/p/android/issues/detail?id=22920

I had the same problem. In my case, I had applied the parent="android:Theme.Light" to values-V14/styles.xml. This made the EditText control to look like a control from android API 2.3.3 and below. But the error message text was all white and hence, was not visible. After some head scratching I figured this out.

Change the parent="android:Theme.Light" to parent="android:Theme.Holo.Light.NoActionBar" (what ever Holo theme). But in the EditText definition add android:background="@android:drawable/edit_text"

My EditText looks like this

<EditText
                android:id="@+id/password"
                style="@style/editTextNormal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/email"
                android:hint="@string/prompt_password"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:singleLine="true"
                android:background="@android:drawable/edit_text" />
LenaYan

If you want to change the text color of the error text view, then you should add this code in your theme files.

For v8:

<item name="android:textColorSecondaryInverse">@android:color/secondary_text_light</item>

For v11:

<item name="android:textColorPrimaryInverse">@android:color/primary_text_light</item>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!