0
0
Android Kotlinmobile~10 mins

Gesture handling in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AUnit
Bfalse
Cnull
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false causes the tap event to be ignored.
Returning null or Unit causes a compile error.
2fill in blank
medium

Complete 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'
AgestureDetector
Bthis
Cv
Devent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of gestureDetector causes errors.
Using 'v' or 'event' does not call the gesture detector.
3fill in blank
hard

Fix 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'
AonSingleTapUp
BonLongPress
ConDoubleTap
DonScroll
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super.onSingleTapUp causes the long press not to work.
Omitting the super call can cause unexpected behavior.
4fill in blank
hard

Fill 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'
A1000f
B500f
C1500f
D200f
Attempts:
3 left
💡 Hint
Common Mistakes
Using too low velocity values causes false positives.
Using integers instead of floats causes type errors.
5fill in blank
hard

Fill 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'
Atrue
Bfalse
Csuper.onDown(e)
DUnit
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.