22
votes

In my form I use setError("") on an EditText field. My Application-Theme extends android:Theme.Holo.
I have manually set an image with a dark background for android:errorMessageBackground and android:errorMessageBackgroundAbove.

And now here's the problem: The text color of the error message is also very dark and not readable.

I tried changing different textColor attributes in my Theme, but I wasn't able to find the correct one.

May anyone could help me, please? Thank you! Chris

6
Ok I found out a solution. I actually couldn't find the specific theme attribute, wich needs to be extended. But one can set <item name="android:textColorPrimaryInverse">#ffffffff</item> to the color nedded. That did the trick for me. This is not colliding with other colors in my application, because I set the text colors for every kind of element in its own style set. Hope this helps.Chris
Add that as an answer to your own question.Macarse
This answer should help you to resolve the issuewaqaslam
Could you tell me how did you manage to set the background using errorMessageBackground? I've set minSDK to 7 and target to 16 and I'm not able to use this attribute in my theme, but all I get is: error: Error: No resource found that matches the given name: attr 'errorMessageBackground'. My question about this: stackoverflow.com/questions/14127710/…scana

6 Answers

1
votes

You can change the text color by using HTML Font Tag.

But for customizing background color, you should make your own custom pop up. For more information, Kindly go through this link:- How to write style to error text of EditText in android?

1
votes

You can try this one:

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

do following in manifest.xml

<resources>
    <style name="LightErrorFix" parent="@android:style/Theme.Light">
         <item name="android:textColorSecondaryInverse">@android:color/secondary_text_light</item>
    </style>
</resources>
0
votes

Assuming you did sth like this:

EditText text = (EditText) findViewById(R.id.myedittext);

you can do the following:

text.setTextColor(Color.parseColor("#FFFFFF"));

or

text.setTextColor(Color.rgb(200,0,0));

or if you want/need alpha:

text.setTextColor(Color.argb(0,200,0,0));

Anyhow, you should specify your colors in your color.xml (wayyy better to be maintained):

<color name="myColor">#f00</color>

and then use it like this:

text.setTextColor(getResources().getColor(R.color.myColor));

Have fun :)

0
votes

set the property android:textColorPrimaryInverse="YourCOLOR" to the color nedded.

0
votes

My response works, is in kotlin.

private fun setErrorOnSearchView(searchView: SearchView, errorMessage: String) {
    val id = searchView.context
            .resources
            .getIdentifier("android:id/search_src_text", null, null)
    val editText = searchView.find<EditText>(id)

    val errorColor = ContextCompat.getColor(this,R.color.red)
    val fgcspan = ForegroundColorSpan(errorColor)
    val builder = SpannableStringBuilder(errorMessage)
    builder.setSpan(fgcspan, 0, errorMessage.length, 0)
    editText.error = builder
}