2

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;

}

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
jelic98
  • 723
  • 1
  • 12
  • 28

1 Answers1

0

Here is what you can do,found out on this link In onCreate of activity, add this.

final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
calendar.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(final View view, final MotionEvent event) {
   if (gestureDetector.onTouchEvent(event)) {
      return false;
    }
    return true;
  }
});

And here is GestureDetector Class

class GestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
    // Trigger the touch event on the calendar
      calendar.onTouchEvent(event);
      return super.onSingleTapUp(event);
    }

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    ViewConfiguration viewConfiguration = ViewConfiguration.get(EventsActivity.this);
    int minSwipeDistance = viewConfiguration.getScaledPagingTouchSlop();
    int minSwipeVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
    int maxSwipeOffPath = viewConfiguration.getScaledTouchSlop();
    if (Math.abs(e1.getY() - e2.getY()) > maxSwipeOffPath) {
        return false;
    }

   if (Math.abs(velocityX) > minSwipeVelocity) {
   // Right to left swipe
      if (e1.getX() - e2.getX() > minSwipeDistance) {
      calendar.nextMonth();
   }

   // Left to right
   else if (e2.getX() - e1.getX() > minSwipeDistance) {
        calendar.previousMonth();
   }

    // Call some app-related functions to update the display
    displayDate(calendar.getMonth(), calendar.getYear());
  }
  return false;
 }
}
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
  • Thanks for the reply. It's not working. I'm trying to implement gestures in my own soft keyboard in order to change screen from qwerty to numeric for example and if you just tap on a single letter that would print that letter. My Keyboard works just fine with onSwipe methods but it's not accurate. That code I've posted is more accurate than yours. So if you can tell me how to tell a difference between touch and swipe. I tried something in that code I've posted but I can only swipe not tap on a single letter. – jelic98 Jun 10 '15 at 07:23