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.
in your textview propeties
in res/color text_selector.xml
When you are deploying the app for Android 11+ (HoneyComb+), you should use
for selected list state. For the earlier versions use the combination of:
Of course don't forget to include the
so the view can get the activated/checked state from a parent list view item
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:
Now you can use the methods others have described above to declare your TextViews' colors using a selector such as:
and I'm sure you're aware, but the selector can be as simple as:
As you can see, @color values are allowed. Hope this helps.
Also - android:state_pressed is used in conjunction with the AdapterView.OnItemClickListener.
Also you may create a
res/color
folder and add a file "text_selector.xml" with the following lines:After that assign in TextView:
In order to make it work on selection use the following code:
Apparently the key is
state_activated="true"
state.