I have 2 buttons with different parents.
I am trying to apply button effect onTouch of the button.
Here is my code:
public class ButtonHighlighterOnTouchListener implements OnTouchListener {
final Button button;
public ButtonHighlighterOnTouchListener(final Button imageButton) {
super();
this.button = imageButton;
}
public boolean onTouch(final View view, final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
button.getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
button.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
button.getBackground().clearColorFilter();
button.invalidate();
break;
case MotionEvent.ACTION_UP:
button.getBackground().clearColorFilter();
button.invalidate();
break;
}
return false;
}
}
It applies the effect to the button, but the problem is since I have 2 buttons, even when I click on one button, the effect is being applied to the other button as well.
Please help me to fix this.
viewinonTouchmethod is, in your case, theButtonis being clicked. So instead ofuse
And the same in other cases of switch.
Edit
Instead, use styles:
Edit 2
Buttonhas a constructor where you can tell which style you want to apply.So you can set
defStyle = android.R.style.Buttonand in yourstyles.xmladdWith that, I think you could initialize your
Buttonwith the style (where you will have your highlighted effect inbackground_button.xml) and add background image as you have right now.