androidresourcesandroid-selector

android:state_pressed is not working


I have the following XML code in res/drawable and I set button background to this drawable. However when I pressed the button it is not not changing the color. Thanks for help

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

<item android:state_enabled="true"
    android:drawable="@color/colorPrimary">
</item>

<item
    android:state_selected="false"
    android:state_pressed="true"
    android:drawable="@color/black">
</item>

Solution

  •     <selector>
        <item android:state_pressed="true" android:drawable="#EDCFE9"/>
        <item android:state_selected="true" android:drawable="#EDCFE9"/>
        <item android:drawable="#603F86"/>
        </selector>
    

    This will change the button color on button press.

    Order matters when specifying the selector fields - whichever selector it matches first, going from top to bottom, will be the one displayed. The default button state should always be specified last.