1

Actually I wanna get singleTapConfirmed() event inside onTouch method . As we all know we can use gestureDetector class for that purpose but I wonder how to do that with onTouch

And I don't wanna call GestureDetector#onTouchEvent I wanna detect that manually.

Below code is there for double tap from this answer but how to get singleTapConfirmed in this code

Code for DoubleTap:

imageView.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View view, MotionEvent event) {
               switch(event.getAction() & MotionEvent.ACTION_MASK)
               {
                   case MotionEvent.ACTION_DOWN:
                       startTime = System.currentTimeMillis();
                       clickCount++;
                       break;
                   case MotionEvent.ACTION_UP:
                       long time = System.currentTimeMillis() - startTime;
                       duration=  duration + time;
                       if(clickCount == 2)
                       {
                           if(duration<= MAX_DURATION)
                           {
                               Toast.makeText(imageView.getContext(), "double tap",Toast.LENGTH_LONG).show();
                           }
                           clickCount = 0;
                           duration = 0;
                           break;
                       }
               }
               return true;
           }
       });
Srinivas Nahak
  • 1,846
  • 4
  • 20
  • 45
  • simply call `GestureDetector#onTouchEvent` inside `onTouch` method – pskink Apr 12 '18 at 06:06
  • @pskink no sir I wanna detect that manually – Srinivas Nahak Apr 12 '18 at 06:08
  • manually? what do you mean? do you think that `GestureDetector` checks single tap "not manually"? what do you want to achieve actually? check for a single click or what? – pskink Apr 12 '18 at 06:14
  • 1
    @pskink sir please check my edited question as we can detect double tap in onTouch how to do that with singleTapConfirmed . Manually means I wanna increase the singleTapConfirmed method's time duration – Srinivas Nahak Apr 12 '18 at 06:15
  • 1
    see `GestureDetector` source file and check `DOUBLE_TAP_TIMEOUT` and `DOUBLE_TAP_MIN_TIME` – pskink Apr 12 '18 at 06:53

2 Answers2

1

It will be fine to use gesture detection with onTouch listener as below

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
     //do something
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        super.onLongPress(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return super.onDoubleTap(e);
    }
});

and use gestureDetector as

 viewToTouch.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return gestureDetector.onTouchEvent(event);
    }
});
Rahul Chaudhary
  • 1,059
  • 1
  • 6
  • 15
0

Try this

 imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           startTime = System.currentTimeMillis();
                   clickCount++;

        }
    });

OnTouchListner return falsh so detact touch event

imageView.setOnTouchListener(new View.OnTouchListener() {
       @Override
       public boolean onTouch(View view, MotionEvent event) {
           switch(event.getAction() & MotionEvent.ACTION_MASK)
           {

               case MotionEvent.ACTION_UP:
                   long time = System.currentTimeMillis() - startTime;
                   duration=  duration + time;
                   if(clickCount == 2)
                   {
                       if(duration<= MAX_DURATION)
                       {
                           Toast.makeText(imageView.getContext(), "double 
                           tap",Toast.LENGTH_LONG).show();
                       }
                       clickCount = 0;
                       duration = 0;
                       break;
                   }
           }
           return false;
       }
   });
mehul chauhan
  • 1,792
  • 11
  • 26