3

I extended the LayerDrawable to automatically change the Alpha Value when pressed my ImageButtons:

    protected class CustomDrawable extends LayerDrawable {

    protected int _pressedAlpha = 60;

    public CustomDrawable(Drawable d) {
        Log.d(TAG, "constructor");
        super(new Drawable[] { d });
    }

    /**
     * Wenn sich der Status auf pressed ändert, soll der Alphawert sich auf _pressedAlpha ändern. Wenn sich der
     * Status von pressed wieder auf normal ändert soll der ursprüngliche Alphawert gesetzt werden
     */
    @Override
    protected boolean onStateChange(int[] states) {

        Log.d(TAG, "status changed: " + states);

        boolean pressed = false;

        for (int state : states) {
            if (state == android.R.attr.state_pressed)
                pressed = true;
        }

        mutate();

        if (pressed) {
            setAlpha(_pressedAlpha);
        } else {
            setAlpha(100);
        }

        invalidateSelf();

        return super.onStateChange(states);
    }
}

The Constructor is called and the Drawable is correct, but when i press the ImageButton, the method onStateChange() is never called. Can someone please help me?

knete
  • 113
  • 1
  • 7

1 Answers1

8

I think you need to override the isStateful() method too. I had a similar problem and adding this fixed it:

@Override
public boolean isStateful() {
    return true;
}

See the code from this answer to a similar question.

Community
  • 1
  • 1
applejuice
  • 81
  • 2