How to detect android double tap?

IBunny picture IBunny · Jan 6, 2014 · Viewed 8.8k times · Source

How to detect the double tap in android? I implement OnDoubleTapListener and wrote this:

public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        if(e.getAction() == 1){
            Toast.makeText(getApplicationContext(),"Double Tap", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

But it is not working. What is the wrong with this?

Answer

venciallee picture venciallee · Jan 6, 2014
public class GestureDoubleTap extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        //some logic
        return true;
    }

}

GestureDoubleTap gestureDoubleTap = new GestureDoubleTap();
gestureDetector = new GestureDetector(this/* context */, gestureDoubleTap);

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }

});