I want to detect a doubletap
on a view, like for example a button
, and then know which view it was. I have seen this similar question but the question they say it is a duplicate of it does not seem to answer my question.
All I can find is to add a GestureDetector
to the activity, and add a OnDoubleTapListener
to it. But that is only triggered if I tap on the background/layout of my screen. It is not triggered when I (double)tap a button
.
This is the code I have inside my onCreate
:
gd = new GestureDetector(this, this); gd.setOnDoubleTapListener(new OnDoubleTapListener() { @Override public boolean onDoubleTap(MotionEvent e) { Log.d("OnDoubleTapListener", "onDoubleTap"); return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { Log.d("OnDoubleTapListener", "onDoubleTapEvent"); //if the second tap hadn't been released and it's being moved if(e.getAction() == MotionEvent.ACTION_MOVE) { } else if(e.getAction() == MotionEvent.ACTION_UP)//user released the screen { } return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Log.d("OnDoubleTapListener", "onSingleTapConfirmed"); return false; } });
Detecting a double tap on Button i.e. whenever the user double taps on any Button how it is detected and according to the Button a response can be added corresponding to it. Here an example is shown in which the double tap on the Button is detected and corresponding to it response is added in the form of toast.
1. Double-click is a term used to describe the process of quickly pressing a mouse button twice while keeping it still. In most cases, a double-click is with the left mouse button and is used to open or execute a file, folder, or software program.
You can achieve this by just using these few lines of codes. It's that simple.
final GestureDetector gd = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){ //here is the method for double tap @Override public boolean onDoubleTap(MotionEvent e) { //your action here for double tap e.g. //Log.d("OnDoubleTapListener", "onDoubleTap"); return true; } @Override public void onLongPress(MotionEvent e) { super.onLongPress(e); } @Override public boolean onDoubleTapEvent(MotionEvent e) { return true; } @Override public boolean onDown(MotionEvent e) { return true; } }); //here yourView is the View on which you want to set the double tap action yourView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gd.onTouchEvent(event); } });
Put this piece of code on the activity or adapter where you want to set the double tap action on your view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With