37

I am struggling with this which apparently is a very simple effect but incredibly haven't found any intutitive way for doing it in Android.

I have a ListView and I managed to customize the background images so the selected item gets highlighted by getting a new background drawable. This I do creating a new style where I set the android:listSelector attribute to point a StateListDrawable where I have specified which drawables to use for every state.

However each ListView item is a LinearLayout where i have two TextViews. My goal is to be able to change the text color of these child views whenever the parent is selected or pressed, at the same time as the background drawable does. I know there is a ColorStateList but haven't been succesful setting that up.

Has anybody succeed getting something like this to work?

Thanks.

1
  • There wasn't a whole lot of information on this - I had the same problem when using a custom background for a ListView that made it unapparent to the user when they had successfully selected an item. Thankfully view saved the day. I left an answer below - great question though as I know your question will better the community.
    – AndrewPK
    Sep 10, 2011 at 20:06

5 Answers 5

69

Neither of these are possible answers when your ListView is compromised of a layout that has multiple views. You need to set your child views to:

android:duplicateParentState="true"

Now you can use the methods others have described above to declare your TextViews' colors using a selector such as:

android:textColor="@drawable/my_row_selector"

and I'm sure you're aware, but the selector can be as simple as:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:color="@color/black" />
</selector>

As you can see, @color values are allowed. Hope this helps.

Also - android:state_pressed is used in conjunction with the AdapterView.OnItemClickListener.

3
  • 1
    Thank you. Also works with fancy choiceModes, etc. I just set up my text selector's states to match those in the list selector. duplicateParentState was the key.
    – dokkaebi
    Dec 18, 2012 at 3:04
  • @AndrewPK dude you're great!
    – Libathos
    Jul 28, 2014 at 9:14
  • it is not working on droid 4.3, 4.4 focus text color is the same as tha normal color
    – lacas
    Dec 17, 2014 at 8:51
23

in your textview propeties

android:textColor="@color/text_selector"

in res/color text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="YOUR_CUSTOM_COLOR" />
    <item android:state_selected="true" android:color="YOUR_CUSTOM_COLOR" />
    <item android:color="YOUR_CUSTOM_COLOR" />
</selector>
3
  • i tried like that and its work great <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#000000" /> <item android:state_selected="true" android:color="#000000" /> <item android:color="#B7B9E7" /> </selector> Dec 24, 2013 at 12:21
  • 4
    In order to work on selection use the following code: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#fff"/> <item android:state_activated="true" android:color="#fff"/> <item android:color="#000" /> </selector> Oct 7, 2014 at 8:30
  • it is not working on droid 4.3, 4.4 focus text color is the same as tha normal color
    – lacas
    Dec 17, 2014 at 8:51
11

In order to make it work on selection use the following code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#fff"/>
    <item android:state_activated="true" android:color="#fff"/>
    <item android:color="#000" />
</selector>

Apparently the key is state_activated="true" state.

0
2

When you are deploying the app for Android 11+ (HoneyComb+), you should use

android:state_activated="true"

for selected list state. For the earlier versions use the combination of:

android:state_checked="true"
android:state_activated="true"

Of course don't forget to include the

android:duplicateParentState="true"

so the view can get the activated/checked state from a parent list view item

1

Also you may create a res/color folder and add a file "text_selector.xml" with the following lines:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:color="#f0f"/>
    <item android:state_pressed="true" android:color="#f0f"/>
    <item android:color="#000"/>
</selector>

After that assign in TextView:

android:textColor="@color/text_selector"
2
  • Great solution, I tried to use a drawable selector which didn't work.
    – Mr. Fish
    Mar 9, 2017 at 12:00
  • @Mr.Fish, Thank you!
    – CoolMind
    Mar 9, 2017 at 12:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.