How to Listen to Doubletap on a View in Android

How detect double tap left and right in android on any type of view

1) You could get coords of tap from MotionEvent object, calculate center of target view (via x,y, height, width) and then handle this data;

2) You could use two transparent overlays and set listeners to them )

Android How to detect double tap?

try this

final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDown(MotionEvent e) {

return true;
}

@Override
public boolean onDoubleTap(MotionEvent e) {

return true;
}
});

Android - Detect doubletap AND tripletap on view

You can try something like this.

Though I would generally recommend against using triple taps as a pattern as it is not something users are generally used to, so unless it's properly communicated to them, most might never know they can triple tap a view. Same goes for double taping actually on mobile devices, it's not always an intuitive way to interact in that environment.

view.setOnTouchListener(new View.OnTouchListener() {
Handler handler = new Handler();

int numberOfTaps = 0;
long lastTapTimeMs = 0;
long touchDownMs = 0;

@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchDownMs = System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
handler.removeCallbacksAndMessages(null);

if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) {
//it was not a tap

numberOfTaps = 0;
lastTapTimeMs = 0;
break;
}

if (numberOfTaps > 0
&& (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) {
numberOfTaps += 1;
} else {
numberOfTaps = 1;
}

lastTapTimeMs = System.currentTimeMillis();

if (numberOfTaps == 3) {
Toast.makeText(getApplicationContext(), "triple", Toast.LENGTH_SHORT).show();
//handle triple tap
} else if (numberOfTaps == 2) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//handle double tap
Toast.makeText(getApplicationContext(), "double", Toast.LENGTH_SHORT).show();
}
}, ViewConfiguration.getDoubleTapTimeout());
}
}

return true;
}
});

Android: How to detect double-tap?

Why aren't you using a Long Press? Or are you using that already for something else? The advantages of a Long Press over a Double Touch:

  • Long Press is a recommeded interaction in the UI Guidelines, Double Touch is not.
  • It's what users expect; a user might not find a Double Touch action as they won't go looking for it
  • It's already handled in the API.
  • Implementing a Double Touch will affect handling of Single Touches, because you'll have to wait to see if every Single Touch turns into a Double Touch before you can process it.

Double Tap in android View

According to gesture listener documentation, you need to return true for onSingleTapConfirmed & onDoubleTap. Otherwise the event is not consumed and gestureDetector.onTouchEvent(me); returns false.



Related Topics



Leave a reply



Submit