2

How to detect the double tap in android? I implement OnDoubleTapListener and wrote this:

public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        if(e.getAction() == 1){
            Toast.makeText(getApplicationContext(),"Double Tap", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

But it is not working. What is the wrong with this?

IBunny
  • 309
  • 9
  • 20
  • Try removing the if block and just make a Toast when you receive the event. That will let us know if the listener is properly attached. – Nathan Walters Jan 06 '14 at 02:33
  • I removed if block and test again. Still I couldn't see the Toast message for double tap. – IBunny Jan 06 '14 at 02:40
  • @Emmanuel : Yah thanks, I saw the given post but I did not understand the given code as the answer. He created new class for that. What I need is to do it inside my activity. I don't have much experience in android and I'm sorry if this is like stupid. – IBunny Jan 06 '14 at 02:52

1 Answers1

6
public class GestureDoubleTap extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        //some logic
        return true;
    }

}

GestureDoubleTap gestureDoubleTap = new GestureDoubleTap();
gestureDetector = new GestureDetector(this/* context */, gestureDoubleTap);

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

});
Gelldur
  • 11,187
  • 7
  • 57
  • 68
venciallee
  • 775
  • 4
  • 19
  • Thanks, But is there any way to do this inside Activity without creating separate class as GestureDoubleTap ? – IBunny Jan 06 '14 at 02:55
  • Anonymous class instead separate class as GestureDoubleTap,otherwise,i'm sorry i have no idea. – venciallee Jan 06 '14 at 03:06
  • Have another question here how to identify particular icon? Using MotionEvent we can get X and Y coordinates but using that how can I get exact icon which has double tap? – IBunny Jan 06 '14 at 05:52
  • first,one icon,one view.second if one view has several icon.you must know the the size of each icon.either,the left point.so, in onDoubleTap(),you just do some logic to calculate x and y in which icon. – venciallee Jan 06 '14 at 06:03