How to define a ColorStateList for a TextView?

When my ListViewItem selected, I want the text to turn white. How to determine this?

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="@color/testcolor1"/> <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" /> <item android:state_enabled="false" android:color="@color/testcolor3" /> <item android:color="@color/testcolor5"/> </selector> 
+26
android colors listview listviewitem
Sep 30 '10 at 3:09
source share
2 answers

Create res / drawable / text_color.xml file:

  <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" /> <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" /> <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" /> <item android:color="#000000" /> </selector> 

Then use @drawable/text_color from xml (or R.drawable.text_color from code) as the text color for list items.

+61
Sep 30 '10 at 6:36
source share

In addition to what was said above, I would like to highlight one point taken from the URL below.

https://developer.android.com/reference/android/content/res/ColorStateList.html

Note. The list of state specifications will be mapped in the order in which they appear in the XML file. For this reason, more specific elements should be placed earlier in the file. An element without a state specification is considered suitable for any set of states and is usually useful as the last element to be used by default.

It is important that you have a broader condition at the bottom of the selector tag. Hope this helps!

0
Sep 19 '16 at 23:20
source share



All Articles