How to catch double tap events in Android using OnTouchListener
That's much easyer:
//variable for storing the time of first click
long startTime;
//constant for defining the time duration between the click that can be considered as double-tap
static final int MAX_DURATION = 200;
if (event.getAction() == MotionEvent.ACTION_UP) {
startTime = System.currentTimeMillis();
}
else if (event.getAction() == MotionEvent.ACTION_DOWN) {
if(System.currentTimeMillis() - startTime <= MAX_DURATION)
{
//DOUBLE TAP
}
}
In your class definition:
public class main_activity extends Activity
{
//variable for counting two successive up-down events
int clickCount = 0;
//variable for storing the time of first click
long startTime;
//variable for calculating the total time
long duration;
//constant for defining the time duration between the click that can be considered as double-tap
static final int MAX_DURATION = 500;
}
Then in your class body:
OnTouchListener MyOnTouchListener = new OnTouchListener()
{
@Override
public boolean onTouch (View v, MotionEvent event)
{
switch(event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
startTime = System.currentTimeMillis();
clickCount++;
break;
case MotionEvent.ACTION_UP:
long time = System.currentTimeMillis() - startTime;
duration= duration + time;
if(clickCount == 2)
{
if(duration<= MAX_DURATION)
{
Toast.makeText(captureActivity.this, "double tap",Toast.LENGTH_LONG).show();
}
clickCount = 0;
duration = 0;
break;
}
}
return true;
}
}
This was adapted from an answer in: DoubleTap in android by https://stackoverflow.com/users/1395802/karn
With the helper class SimpleGestureListener that implements the GestureListener and OnDoubleTapListener you dont need much to do.
yourView.setOnTouchListener(new OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(Test.this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("TEST", "onDoubleTap");
return super.onDoubleTap(e);
}
... // implement here other callback methods like onFling, onScroll as necessary
});
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TEST", "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");
gestureDetector.onTouchEvent(event);
return true;
}});