Text color in text does not change when disabled

When I call setEnabled(false) on a TextView object, the color of the text does not change. I expected it to be changed to gray. If I delete the line android:textColor in my XML file, it will return to its normal state.

Any ideas?

+47
android colors android-textview
Aug 27 '09 at 17:02
source share
1 answer

I think that happens because, since you override the default color of the text, it does not inherit other textcolor styles. Try creating a ColorStateList for it and setting the textColor attribute for it instead of color.

In a color file (e.g. res / color / example.xml):

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:color="@color/disabled_color" /> <item android:color="@color/normal_color"/> </selector> 

then in your layout:

 <TextView android:text="whatever text you want" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/example" /> 

Notice, I have not done this for a while, and I am printing a lot from memory, so a little configuration may be required. The ColorStateList documents (linked above) have a more glued example for the XML color file.

+117
Aug 29 '09 at 1:32
source share
— -



All Articles