'Implement GestureDetector.OnDoubleTapListener properly on custom view
I have a CustomAutoCompleteTextView
and I want to be able to call showDropDown();
when the user double-tap
on it.
As the title says implements GestureDetector.OnDoubleTapListener
sounds like a good solution, but once added the mandatory methods to my custom view I have no idea on how to set the whole thing up
These are the methods to implement:
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
I want to archive something like this:
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
showDropDown();//AutoCompleteTextView's this line is enough but the event is never triggered
return super.doYourThing();
}
Solution 1:[1]
I've found a better way than implements GestureDetector.OnDoubleTapListener
to archive this, notice that this solution is for implement double tap inside a custom view and all of its instances
boolean firstTouch = false;
long time;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == event.ACTION_DOWN){
if(firstTouch && (System.currentTimeMillis() - time) <= 300) {
firstTouch = false;
showDropDown();//In my case I want to showDropDown() change this line for whatever you want to do
} else {
firstTouch = true;
time = System.currentTimeMillis();
return super.onTouchEvent(event);
//return false;Use this if you dont want to call default onTouchEvent()
}
}
return super.onTouchEvent(event);
//return false;Use this if you dont want to call default onTouchEvent()
}
Solution 2:[2]
1) create a new GestureDetector()
detector = new GestureDetector(this,new OnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
showDropDown()
return true;
}
2) Append your CustomAutoCompleteTextView
with a OnTouchListener
txt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return false;
}
});
Solution 3:[3]
This is what worked well for me:
tapDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener());
tapDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e){
//Notified when a single-tap occurs.
Log.w("Debug","Single tap event occurred in view with ID: "+ getId());
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.w("Debug","Double tap event occurred in view with ID: "+ getId());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
//Notified when an event within a double-tap gesture occurs, including the down, move, and up events.
return true;
}
});
As mentioned in the previous solution:
txt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
tapDetector.onTouchEvent(event);
return false;
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Westside Tony |
Solution 2 | |
Solution 3 | user1060873 |