When it came to a point wherein I wanted my Button and ImageButton to show an image that it was pressed, most of the forum answers suggested using different image drawable resources for pressed, focused and normal state.

Then I thought to myself, that is pretty much a hassle. I do not like to create a different image for any of those. So I checked the iTouch Facebook app to see how the image looks like when pressed. Nothing special really. It just changed tint while on some buttons, their colors changed.

If you feel the same sentiment as I do by wanting to use only one image as a drawable resource, then you only need to change the tint color of the ImageButton by changing its color filter.

To do this, you need to set the ImageButton an OnTouchListener class. Check the code below.

What this does is whenever you tap on an ImageButton, it changes the image’s filter to a dark grayish bit of color. You can change it to any other color you like. 155, 155, 155 is the RGB value for my gray color.

If you check the first parameter, the values are 150 and 0. 0 means that there should be no color filter applied to it, hence displaying the ImageButton as normal (or you can always use null as the value). The 150 value shows the dark gray color of the ImageButton. 255 is the maximum value.

Easy right? Now you do not need to create separate images for the states.

Related Posts Plugin for WordPress, Blogger...