Android Button background image pressed/highlighted and disabled states without using multiple images
Mar 18, 2012
2 minute read
In Android, if you provide custom background images for buttons, you will lose the pressed and disabled
image effects. The common way to fix that is to provide additional images for those states. I’m lazy and
I find this inconvenient especially during the prototyping phase of app development.
I’ve always liked the way iOS automatically handles pressed and disabled states for custom button
backgrounds so I made a Button subclass that automatically darkens the background image when the
button is pressed, and makes the background transparent when it is disabled. This is done by using a
custom LayerDrawable
for the button that contains the original background Drawable. The LayerDrawable has to be
stateful
and should change the background properties depending on the current state in
onStateChange().
The full source explains it better:
To use this, just replace your original button declarations like this: