3

My custom selector like below doesn't work as expected, I don't see the pressed state at all. Any tip? thanks

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"

    >
    <!-- this state button has special design:
        + enabled state has white text on green background
        + disabled state has grey text on greyer background
    -->


    <item android:state_enabled="true"
        android:drawable="@drawable/cb_shape_button_primary"
        android:color="@color/white"

        />
    <item android:state_pressed="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />

    <item android:state_focused="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />
    <item android:state_enabled="false"
        android:drawable="@drawable/cb_shape_button_flat"
        android:color="@color/grey_subtext"
        />

</selector>
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126
  • linked https://stackoverflow.com/questions/14042866/state-list-drawable-and-disabled-state – TT-- Oct 30 '18 at 18:37

4 Answers4

6

Try moving state_pressed to top like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"

>
<!-- this state button has special design:
    + enabled state has white text on green background
    + disabled state has grey text on greyer background
-->

<item android:state_pressed="true"
    android:drawable="@drawable/cb_shape_button_pressed"
    android:color="@color/white"

    />

<item android:state_enabled="true"
    android:drawable="@drawable/cb_shape_button_primary"
    android:color="@color/white"

    />

<item android:state_focused="true"
    android:drawable="@drawable/cb_shape_button_pressed"
    android:color="@color/white"

    />
<item android:state_enabled="false"
    android:drawable="@drawable/cb_shape_button_flat"
    android:color="@color/grey_subtext"
    />

Edit:
As @forcewill mentioned, Just to note that the reason behind moving the pressed state to the top has to do with the fact that Android will use the first state that match, in your case the widget is enabled and pressed simultaneous so it was matching the enabled state first.

Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
  • 3
    Just to note that the reason behind moving the pressed state to the top has to do with the fact that Android will use the first state that match, in your case the widget is enabled and pressed simultaneous so it was matching the enabled state first – forcewill Sep 03 '15 at 12:37
2

The selector XML tag always picks the first matching item. This means if the View is enabled it will always show @drawable/cb_shape_button_primary. A general rule of thumb with selectors is that your "default" item should be the last one in the list.

Try moving your default state last and see if that resolves your issues.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"

    >
    <!-- this state button has special design:
        + enabled state has white text on green background
        + disabled state has grey text on greyer background
    -->

    <item android:state_pressed="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />

    <item android:state_focused="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />
    <item android:state_enabled="false"
        android:drawable="@drawable/cb_shape_button_flat"
        android:color="@color/grey_subtext"
        />

   <item android:state_enabled="true"
        android:drawable="@drawable/cb_shape_button_primary"
        android:color="@color/white"

        />

</selector>
cyroxis
  • 3,661
  • 22
  • 37
0

Try this,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/select_item" android:state_checked="true" android:state_focused="true" />
<item android:drawable="@drawable/unselect_item" android:state_checked="false" android:state_focused="true" />
<item android:drawable="@drawable/unselect_item" android:state_checked="false" />
<item android:drawable="@drawable/select_item" android:state_checked="true" />

Das
  • 141
  • 13
0

For Simple Select And Unselect Use This

<selector><item android:state_checked="false" android:drawable="@drawable/radiobtnunselected"/><item android:state_checked="true" android:drawable="@drawable/radiobtnselected"/></selector>

And Set Your Image At android:drawable

True: Selected

False: Unselected

Maulik Santoki
  • 532
  • 4
  • 14