Search code examples
androidandroid-event

Detecting whether touch event has occurred in android?


I am developing an android application, in which, I am changing the image continuously in intervals. Before every change, I need to check if touch event has occurred or not? If yes, then which event has occurred, DOWN or UP?

In my search I only found how to add touch event and check which event occurred, but I couldn't find a solution which would enable me to know if event has occurred or not.

Can Someone Help me in this?


Solution

  • Copy paste this in your java code

    public class yourActivity extends FragmentActivity implements View.OnTouchListener {
    ImageView yourpic;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    yourpic = (ImageView) findViewById(R.id.yourpicID);
     yourpic.setOnTouchListener(this);
    }
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    
        ImageView view = (ImageView) v;
    
    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:                                  
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                }
                break;
    }
    return true;
    }