Aggressor Aggressor - 4 months ago 26
Java Question

Android ImageButton Pressed State Doesn't Change

I am programatically setting the pressed state of an ImageButton.

Problem is when I press on it, the state does not change at all. I tried using

state_focused
and a few others but the button does not change at all. Do you know why my ImageButton is not changing its image when pressed?

_pauseButton = new ImageButton(_context);

StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] {},
getResources().getDrawable(R.drawable.pause));
stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pausepressed));
_pauseButton.setImageDrawable(stateListDrawable);


The default state image does show up correctly, but when its pressed, there is no change.

Answer

I had to set the default state LAST.

    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
            getResources().getDrawable(R.drawable.pausepressed));
    stateListDrawable.addState(new int[] {},
            getResources().getDrawable(R.drawable.pause));

Looks like a bug in android and I've setup a report:

https://code.google.com/p/android/issues/detail?id=217939&can=4&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened