Search code examples
androidsurfaceviewgesture

How to implement double tap for surface view in android


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


Solution

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