androidandroid-layoutandroid-buttonnine-patchstatelistdrawable

StateListDrawable not showing pressed state


I simply want to create a custom 9-Patch Button. So here is what I've got:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/yellow_button_background_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/yellow_button_background_pressed"
          android:state_focused="true" />
    <item android:drawable="@drawable/yellow_button_background_default" />
</selector>

with the respective images: yellow_button_background_default(default), yellow_button_background_pressed(pressed)

and the layout

<Button
    android:id="@+id/answerAButton"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/answer_a"
    android:background="@drawable/yellow_button_background"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/black"/>

Now, when I touch the button, nothing happens. I tried an emulator and the real phone, but they always show the default graphic. The individual graphics work fine on their own, but somehow the StateListDrawable XML is not working as intended.

Any help is very much appreciated.

Solution: The code is ok, but the button was disabled by another piece of code. Therefore, it could not be clicked.


Solution

  • The problem was neither the StateListDrawable nor the Layout. As Luksprog pointed out, the button never received an onClick-event, since it had been disabled in code. Silly me had forgotten, that the event to re-enable the buttons was never triggered in my current debug-setup.