How to detect Swipe and Tap in onTouch
method? I want one of two actions to be executed when I swipe and other when I just tap. Need to know how to dell the difference between tap and swipe.
Here is my code:
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mGestureDetector.onTouchEvent(event);
}
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
float deltaX = x2 - x1;
float deltaY = y2 - y1;
if (Math.abs(deltaX) > MIN_DISTANCE) {
mGestureDetector.onTouchEvent(event);
}else if (Math.abs(deltaX) < MIN_DISTANCE) {
mGestureDetector.onTouchEvent(event);
}else if (Math.abs(deltaY) > MIN_DISTANCE) {
mGestureDetector.onTouchEvent(event);
}else if (Math.abs(deltaY) < MIN_DISTANCE) {
mGestureDetector.onTouchEvent(event);
}
break;
}
return true;
}
Here is what you can do,found out on this link In onCreate of activity, add this.
And here is GestureDetector Class