0

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.

Aggressor
  • 13,025
  • 19
  • 96
  • 173

2 Answers2

0

Try to add this line:

_pauseButton.setPressed(boolean)

If not help, try to read this article: Android : How to update the selector(StateListDrawable) programmatically

Community
  • 1
  • 1
Huy
  • 3,262
  • 5
  • 19
  • 37
0

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

Aggressor
  • 13,025
  • 19
  • 96
  • 173