Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Double Tap

How do I use double tap events in an activity? The double tap event or long click method working (though the method is getting overriden) I've just put in a toast message in each of these methods and yet no result! Can you help?

like image 597
user2007668 Avatar asked Jan 24 '13 13:01

user2007668


1 Answers

The easiest way to do a double-tap is to detect it with a GestureDetector. The "trick" is to be sure you delegate the Activity's onTouchEvent to the GestureDetector's 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);
    }
}
like image 133
Scott Stanchfield Avatar answered Oct 27 '22 03:10

Scott Stanchfield