Android How to detect double tap?
try this
final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
});
Double Tap event in an Activity
onDoubleTap function Use specific element, like an ImageView id, is 'selectedImage' & run this function name is 'selectImage()' full code:
findViewById(R.id.selectdImage).setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(getApplicationContext(), "onDoubleTap", Toast.LENGTH_SHORT).show();
selectImage(nView);
return super.onDoubleTap(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d("onSingleTapConfirmed", "onSingleTap");
return false;
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
});
Android avoid calling single tap twice for double tap, without using onSingleTapConfirmed
The way I would do it is set a state variable as a long and call it timeOfLastTouch and set it to 0. Then in each OnClickListener function put this code in their
long currentTime = System.currentTimeMillis();
if (timeOfLastTouch + 100 < currentTime) {
// code
timeOfLastTouch = System.currentTimeMillis();
}
Android detecting double tap without single tap first
I'm confused on what you're trying to achieve. The double tap method onDoubleTap()
isn't called when the single tap occurs. On the other hand, for every double tap there are two associated single tap onSingleTapUp()
method calls.
If you want to distinguish them, you can use onSingleTapConfirmed()
which is triggered when the gesture detector is sure the tap is single. See reference:
Notified when a single-tap occurs.
Unlike
OnGestureListener.onSingleTapUp(MotionEvent)
, this will only be called after the detector is confident that the user's first tap is not followed by a second tap leading to a double-tap gesture
You can then use this method call in combination with a boolean
flag to essentially detect any type of single/double tap.
Android distinguish between tap and double tap
I would suggest that you switch to the SimpleGestureListener and use the onDoubleTap() and onSingleTapConfirmed() methods.
Android: How to detect double-tap?
Why aren't you using a Long Press? Or are you using that already for something else? The advantages of a Long Press over a Double Touch:
- Long Press is a recommeded interaction in the UI Guidelines, Double Touch is not.
- It's what users expect; a user might not find a Double Touch action as they won't go looking for it
- It's already handled in the API.
- Implementing a Double Touch will affect handling of Single Touches, because you'll have to wait to see if every Single Touch turns into a Double Touch before you can process it.
Related Topics
Add Shadow to Custom Shape on Android
Debugging SQLite Database on the Device
How to Set Default App Launcher Programmatically
Android Facebook 4.0 Sdk How to Get Email, Date of Birth and Gender of User
How to Implement Cookie Handling on Android Using Okhttp
How to Get the Current Page Number of a Viewpager for Android
Converting Yuv->Rgb(Image Processing)->Yuv During Onpreviewframe in Android
Android Spanned, Spannedstring, Spannable, Spannablestring and Charsequence
Force Update of an Android App When a New Version Is Available
Recyclerview Inside Scrollview Not Scrolling Smoothly
How to Clear an Imageview in Android
How to Get an Uri of an Image Resource in Android
Best Practice for Defining Button Events in Android
Get Context of Popupmenu Like Contextmenu