If it is ImageButton, and you do not want to use several available for each pressed / not pressed state, you can use the filter of color images. This solution is similar to that used by Omar.
Create an OnTouchListener by changing the color filter on touch.
public class ButtonHighlighterOnTouchListener implements OnTouchListener { final ImageButton imageButton; public ButtonHighlighterOnTouchListener(final ImageButton imageButton) { super(); this.imageButton = imageButton; } public boolean onTouch(final View view, final MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Assign a button to this listener:
myButton = (ImageButton) findViewById(R.id.myButton); myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton));
Update
Improved class for applying marker to ImageView, ImageButton or TextView through its composite Drawable.
public class ButtonHighlighterOnTouchListener implements OnTouchListener { private static final int TRANSPARENT_GREY = Color.argb(0, 185, 185, 185); private static final int FILTERED_GREY = Color.argb(155, 185, 185, 185); ImageView imageView; TextView textView; public ButtonHighlighterOnTouchListener(final ImageView imageView) { super(); this.imageView = imageView; } public ButtonHighlighterOnTouchListener(final TextView textView) { super(); this.textView = textView; } public boolean onTouch(final View view, final MotionEvent motionEvent) { if (imageView != null) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { imageView.setColorFilter(FILTERED_GREY); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { imageView.setColorFilter(TRANSPARENT_GREY);
LG Jan 11 '13 at 13:03
source share