State_Selected is not working although state_pressed works

10,176

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!

Share:
10,176
alicanbatur
Author by

alicanbatur

Mobile Dev.

Updated on September 16, 2022

Comments

  • alicanbatur
    alicanbatur over 1 year

    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
    alicanbatur almost 11 years
    Thanks, that answer is really useful. stackoverflow.com/questions/9729517/…
  • User
    User about 7 years
    @sleeke The idea is to set the view as checked can you please explain this?