I have an image and I want to create a button click like effect - when the button is touched it should change colour for an instant.
I have looked at the links below but it's not working for me:
How can I give an imageview click effect like a button on Android?
Maybe it's my android version, which is Lollipop. Although I have tried suggestions in links above and it's not working. My image does not change and stays same colour.
Here is my code:
//into the toolbar, inflate the back button and Populisto title,
//which we find in toolbar_custom_view_layout.xml
View logo = getLayoutInflater().inflate(R.layout.toolbar_custom_view_layout, null);
toolbar.addView(logo);
ImageView backButton = (ImageView) logo.findViewById(R.id.back_arrow_id);
backButton.setBackgroundColor(Color.rgb(100, 100, 50));
//set the ontouch listener
backButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
ImageView view = (ImageView) v;
view.getDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
//clear the overlay
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
return false;
}
});