Question

In a view I want to detect both single tap and double tap. It works fine with me. I can detect both single and double tap. Please find my code

@Override
    public boolean onTouch(View view, MotionEvent me) {
        // No dragging during animation at the moment.
        // TODO: Stop animation on touch event and return to drag mode.
        boolean result = true;
        result = gestureDetector.onTouchEvent(me);//return the double tap events
        if(result)
            return false;

switch (me.getAction()) {
        case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
}


private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private String fileName;



        @Override
         public boolean onSingleTapConfirmed(MotionEvent e) {
          // TODO Auto-generated method stub

          return false;
         }

        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {

            float x = e.getX();
                float y = e.getY();
                if(player!=null && player.isPlaying())
                {
                    player.stop();
                }
                else
                {
                    setFileName(x);
                    player = new AudioPlayer(fileName);
                    player.play();
                }


            return true;
        }


    }

I am doing some code in Action_UP event. So even when I do a double tap, for the first tap the ACTION_UP event is called and the corresponding code is executed. I want to prevent this from happening. How can I do this. I just want to make sure that the ACTION_UP is called only when the user is done with his gesture. Not immediately after his partial gesture . In this case its being called on finger up of first tap.

How can I make this work in android?

Thanks

Was it helpful?

Solution

According to gesture listener documentation, you need to return true for onSingleTapConfirmed & onDoubleTap. Otherwise the event is not consumed and gestureDetector.onTouchEvent(me); returns false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top