Android Double Tap

How to use double click events in activity? Double click event or long click working (although the method becomes excessive) I just added a toast message in each of these methods and yet no result! You can help?

+5
source share
1 answer

The easiest way to double-click is to detect it using the GestureDetector. The trick is to make sure that you delegate the Activity onTouchEvent to the GestureDetector onTouchEvent:

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Toast.makeText(MainActivity.this, "double tap", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        return super.onTouchEvent(event);
    }
}
+9
source

All Articles