0

I have seen on Google how to perform actions on MotionEvents of ImageView, what i want to do is change the image when user press the ImageView and reset to previous image when user release the ImageView, i am performing some reset code in my activity i just want an effect when user press the imageview.

Here is what i have done,

    iv_reset.setOnTouchListener(new OnTouchListener() 
    {           
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            // TODO Auto-generated method stub
            switch (event.getAction()) 
            {
                case MotionEvent.ACTION_DOWN:
                    iv_reset.setImageResource(R.drawable.ic_action_reset_pressed);
                    iv_reset.invalidate();

                    break;
                case MotionEvent.ACTION_UP:
                    iv_reset.setImageResource(R.drawable.ic_action_reset);
                    iv_reset.invalidate();
                    resetAll();
                    break;                      
                default:
                    break;
            }

The problem is when i touch the ImageView (iv_reset) it changes the image, but when i release the touch it remains changed and not resetting to previous image which should be done in MotionEvent.ACTION_UP: case i think.

Any help please.

Darshan
  • 515
  • 1
  • 3
  • 16

1 Answers1

2

You're making your life really difficult. Scrap your onTouchListener and use a state list drawable instead.

In your case, just set the ImageView src attribute to this drawable (defined in XML inside your drawables folder):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_action_reset_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_action_reset" />
</selector>
0101100101
  • 5,786
  • 6
  • 31
  • 55