Home » Android » surfaceview – How to implement double tap for surface view in android

surfaceview – How to implement double tap for surface view in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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;
        }
    });

Answer:

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