0

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?

setColorFilter not working

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;
      }

    });
CHarris
  • 2,693
  • 8
  • 45
  • 71
  • 1
    Can you please try changing your return statement into true and see the response then. – Nero Sep 30 '18 at 23:35
  • Ha yes, that does it. I was using the most upvoted answer in this post:https://stackoverflow.com/questions/4617898/how-can-i-give-an-imageview-click-effect-like-a-button-on-android which has `return false;` Does that mean it should be edited? Also, I changed `Mode.MULTIPLY` to `Mode.SRC_ATOP` but wasn't working until I changed to `return true;` Post as answer and I'll gladly accept - if not right now, will do in the morning. – CHarris Sep 30 '18 at 23:45

2 Answers2

1
return false;

It doesn't seem to accept the changes as with the statement you've wrote. Please change it into the following:

return true;

Any more questions, feel free to ask :)

Nero
  • 1,058
  • 1
  • 9
  • 25
0

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.

Wrap your ImageView in a CardView & set CardView's foreground as ?selectableItemBackgroundBorderless, it'll give you a ripple effect.

Darshan
  • 4,020
  • 2
  • 18
  • 49