0
0
Android Kotlinmobile~20 mins

Gesture handling in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Gesture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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
}
APrints "Single tap detected" once per tap
BThrows a runtime exception
CDoes not print anything on tap
DPrints "Single tap detected" multiple times per tap
Attempts:
2 left
💡 Hint
Consider how GestureDetector.SimpleOnGestureListener handles tap events.
lifecycle
intermediate
1:30remaining
GestureDetector and Activity Lifecycle
What happens if you create a GestureDetector inside onCreate but forget to set the OnTouchListener on the view?
AGestureDetector will detect gestures automatically without OnTouchListener
BGestureDetector will not receive touch events, so no gestures are detected
CApp will crash with NullPointerException
DGestureDetector will detect gestures but with delay
Attempts:
2 left
💡 Hint
Think about how touch events reach GestureDetector.
🔧 Debug
advanced
2: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 }
AGestureDetector must be recreated on every touch event
BonFling requires overriding onScroll as well
CThe velocity thresholds for fling are not met because the swipe is too slow
DThe view does not have focus
Attempts:
2 left
💡 Hint
Consider what conditions GestureDetector uses to recognize a fling.
🧠 Conceptual
advanced
2:00remaining
Difference Between onTouchEvent and OnTouchListener
Which statement correctly describes the difference between overriding View.onTouchEvent and setting an OnTouchListener?
AOnTouchListener is called before onTouchEvent and can consume the event to prevent onTouchEvent from running
BonTouchEvent is a method inside the View class to handle touch events; OnTouchListener is an interface to listen for touch events externally
CBoth are the same and interchangeable
DonTouchEvent only handles multi-touch, OnTouchListener only handles single touch
Attempts:
2 left
💡 Hint
Think about event propagation and consumption in Android touch system.
navigation
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?
AWrap ScrollView in a FrameLayout to block gestures
BDisable scrolling in ScrollView and handle all gestures in ViewPager
CUse GestureDetector only on ScrollView and ignore ViewPager gestures
DOverride onInterceptTouchEvent in the parent ViewPager to detect vertical scrolls and avoid intercepting them
Attempts:
2 left
💡 Hint
Think about how parent views can decide to intercept touch events.