Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to detect a tap gesture on a View.
Android Kotlin
val gestureDetector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent?): Boolean {
Toast.makeText(this@MainActivity, "Tapped!", Toast.LENGTH_SHORT).show()
return [1]
}
}) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false causes the tap event to be ignored.
Returning null or Unit causes a compile error.
✗ Incorrect
Returning true means the tap event was handled successfully.
2fill in blank
mediumComplete the code to set a touch listener that passes events to the gesture detector.
Android Kotlin
myView.setOnTouchListener { v, event ->
[1].onTouchEvent(event)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of gestureDetector causes errors.
Using 'v' or 'event' does not call the gesture detector.
✗ Incorrect
The gestureDetector handles the touch events to detect gestures.
3fill in blank
hardFix the error in the gesture listener to detect a long press gesture.
Android Kotlin
val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
override fun onLongPress(e: MotionEvent?) {
super.[1](e)
Toast.makeText(context, "Long pressed", Toast.LENGTH_SHORT).show()
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super.onSingleTapUp causes the long press not to work.
Omitting the super call can cause unexpected behavior.
✗ Incorrect
Calling super.onLongPress ensures the base class handles the long press event properly.
4fill in blank
hardFill both blanks to detect a fling gesture with velocity check.
Android Kotlin
override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
if (velocityX > [1] && velocityY > [2]) {
Toast.makeText(context, "Fling detected", Toast.LENGTH_SHORT).show()
return true
}
return false
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using too low velocity values causes false positives.
Using integers instead of floats causes type errors.
✗ Incorrect
Velocity thresholds like 1000f and 500f help detect a fast enough fling gesture.
5fill in blank
hardFill all three blanks to create a gesture detector that recognizes double taps and single taps.
Android Kotlin
val gestureDetector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
override fun onDoubleTap(e: MotionEvent?): Boolean {
Toast.makeText(context, "Double tap", Toast.LENGTH_SHORT).show()
return [1]
}
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
Toast.makeText(context, "Single tap", Toast.LENGTH_SHORT).show()
return [2]
}
override fun onDown(e: MotionEvent?): Boolean {
return [3]
}
}) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false in tap handlers causes gestures to be ignored.
Returning Unit causes compile errors.
Not calling super.onDown causes gesture detection issues.
✗ Incorrect
Return true in onDoubleTap and onSingleTapConfirmed to indicate event handled; onDown should call super.onDown.