8

Please tell how to implement double tap for SurfaceView in Android using gesture detector. Can anybody provide code example?

keaukraine
  • 5,315
  • 29
  • 54
mohan
  • 13,035
  • 29
  • 108
  • 178

2 Answers2

16

You could try following.. actually i tested this and it works pretty well:

1) Extend GestureDetector.SimpleOnGestureListener and override it's onDoubleTap() method:

    class DoubleTapGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.d("TAG", "Double Tap Detected ...");
            return true;
        }

    }

2) Instantiate the GestureDetector:

final GestureDetector mGesDetect = new GestureDetector(this, new DoubleTapGestureDetector());

3) Set an OnTouchListener on your SurfaceView, override its onTouch() method and call the onTouchEvent() on your GestureDetector object:

    surfview.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mGesDetect.onTouchEvent(event);
            return true;
        }
    });
lukuluku
  • 4,344
  • 3
  • 30
  • 35
0

I have tried above solution. But, it didn't work for my case. I got the touch event in surface view touch listener, but didn't find any callback either in onDoubleTap or onSingleTapConfirmed methods. My Code is given below:

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // code here
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            // code here
            return super.onSingleTapConfirmed(e);
        }

    });

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

When I made the surface view clickable, it worked.

 surfaceView.setClickable(true);

Detailed explanation can be found in this link