Recognizing tap and other common gestures
Unlike the Event Listeners described in the previous recipe, gestures require a two-step process:
- Gather movement data
- Analyze the data to determine whether it matches a known gesture
Step 1 begins when the user touches the screen, which fires the onTouchEvent()
callback with the movement data sent in a MotionEvent
object. Fortunately, Android makes step 2, analyzing the data, easier with the GestureDetector
class, which detects the following gestures:
onTouchEvent()
onDown()
onFling()
onLongPress()
onScroll()
onShowPress()
onDoubleTap()
onDoubleTapEvent()
onSingleTapConfirmed()
This recipe will demonstrate using GestureDetector.SimpleOnGestureListener
to recognize the touch and double tap gestures.
Getting ready
Create a new project in Android Studio and call it CommonGestureDetector
. Use the default Phone & Tablet
options and select Empty Activity
when prompted for the Activity Type
.
How to do it...
We will be using the activity itself to detect gestures, so...