Challenge - 5 Problems
Gesture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Detecting a Single Tap Gesture
What will be the output when the user performs a single tap on the view with the following code?
Android Kotlin
val gestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent?): Boolean {
return true
}
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
println("Single tap detected")
return true
}
})
view.setOnTouchListener { _, event ->
gestureDetector.onTouchEvent(event)
true
}Attempts:
2 left
💡 Hint
Consider how GestureDetector.SimpleOnGestureListener handles tap events.
✗ Incorrect
The onSingleTapConfirmed method is called once per confirmed single tap, so the message prints once per tap.
❓ lifecycle
intermediate1:30remaining
GestureDetector and Activity Lifecycle
What happens if you create a GestureDetector inside onCreate but forget to set the OnTouchListener on the view?
Attempts:
2 left
💡 Hint
Think about how touch events reach GestureDetector.
✗ Incorrect
GestureDetector needs touch events passed to it via OnTouchListener; without it, it won't detect gestures.
🔧 Debug
advanced2:30remaining
Why does onFling not trigger?
Given this code snippet, why might the onFling gesture not trigger as expected?
val gestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent?): Boolean {
return true
}
override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
println("Fling detected")
return true
}
})
view.setOnTouchListener { _, event ->
gestureDetector.onTouchEvent(event)
true
}
Attempts:
2 left
💡 Hint
Consider what conditions GestureDetector uses to recognize a fling.
✗ Incorrect
onFling triggers only if the swipe velocity exceeds a certain threshold; slow swipes won't trigger it.
🧠 Conceptual
advanced2:00remaining
Difference Between onTouchEvent and OnTouchListener
Which statement correctly describes the difference between overriding View.onTouchEvent and setting an OnTouchListener?
Attempts:
2 left
💡 Hint
Think about event propagation and consumption in Android touch system.
✗ Incorrect
OnTouchListener's onTouch method is called before View's onTouchEvent; returning true consumes the event and stops further processing.
expert
3:00remaining
Gesture Navigation Conflict Resolution
In an app with a horizontal ViewPager and a vertical ScrollView inside each page, what is the best way to handle gesture conflicts so both horizontal swipes and vertical scrolls work smoothly?
Attempts:
2 left
💡 Hint
Think about how parent views can decide to intercept touch events.
✗ Incorrect
Overriding onInterceptTouchEvent allows the ViewPager to decide when to intercept horizontal swipes and let vertical scrolls pass to ScrollView.