9

i have a problem. First check my code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/clickedtext" />
    <item android:state_pressed="true" android:color="@color/clickedtext" />
    <item  android:color="@color/defaulttext" />
</selector>

Here, defaulttext color appears at default state. When i clicked, text color in the listitem changes. But color of the text does not change when an item selected.

I tried some stuff, but i cannot handle this problem. What should i do ?

alicanbatur
  • 2,172
  • 1
  • 28
  • 36
  • what happens in the Java part? Do you call setSelected(true) on your view? – znat Apr 02 '13 at 15:08
  • i use it on ListView, is it needed to use at textview too ? – alicanbatur Apr 02 '13 at 15:52
  • There are some issues with `state_selected` on certain devices. You might have a look here for some ideas: http://stackoverflow.com/questions/14237978/listview-item-state-selected-is-lost-on-samsung-device – David Wasser Apr 02 '13 at 16:08

2 Answers2

20

I was also searching for an answer for this when using a ListView, and had to trawl through a lot of posts here before finding this one which I believe is the best option, and worked like a charm.

The idea is to set the view as checked, and then use state_activated rather than state_selected to perform your selection. If you're using a ListView or similar you'll also need to call setChoiceMode(ListView.CHOICE_MODE_SINGLE) to make sure only one item is checked. See the post for more info

I hope this helps someone find the solution faster than I did!

Community
  • 1
  • 1
head in the codes
  • 1,159
  • 12
  • 24
  • Thanks, that answer is really useful. http://stackoverflow.com/questions/9729517/showing-the-current-selection-in-a-listview/9729579#9729579 – alicanbatur Jun 19 '13 at 14:40
  • @sleeke **The idea is to set the view as checked** can you please explain this? – User Mar 28 '17 at 12:26
0

when using a custom listitem-layout: set

android:background="?android:attr/activatedBackgroundIndicator"

as background

wutzebaer
  • 14,365
  • 19
  • 99
  • 170