0
0
Android Kotlinmobile~5 mins

Gesture handling in Android Kotlin

Choose your learning style9 modes available
Introduction

Gesture handling lets your app respond when users touch, swipe, or tap the screen. It makes apps interactive and fun to use.

You want to detect when a user taps a button or image.
You want to respond to a swipe to change screens or items.
You want to detect a long press to show extra options.
You want to zoom in or out using pinch gestures.
You want to drag an object around the screen.
Syntax
Android Kotlin
val gestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        // Handle tap
        return true
    }

    override fun onLongPress(e: MotionEvent?) {
        // Handle long press
    }

    override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
        // Handle swipe
        return true
    }
})

view.setOnTouchListener { v, event ->
    gestureDetector.onTouchEvent(event)
    true
}

GestureDetector helps detect common gestures like tap, long press, and swipe.

Override only the gesture methods you need inside SimpleOnGestureListener.

Examples
This code shows a message when the user taps the screen once.
Android Kotlin
override fun onSingleTapUp(e: MotionEvent?): Boolean {
    Toast.makeText(context, "Tapped!", Toast.LENGTH_SHORT).show()
    return true
}
This code shows a message when the user presses and holds the screen.
Android Kotlin
override fun onLongPress(e: MotionEvent?) {
    Toast.makeText(context, "Long Pressed!", Toast.LENGTH_SHORT).show()
}
This code detects a fast swipe to the right and shows a message.
Android Kotlin
override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
    if (velocityX > 1000) {
        Toast.makeText(context, "Swiped Right!", Toast.LENGTH_SHORT).show()
    }
    return true
}
Sample App

This app shows a full screen text. When you tap, long press, or swipe left or right on the text, it shows a small popup message describing the gesture.

Android Kotlin
class MainActivity : AppCompatActivity() {
    private lateinit var gestureDetector: GestureDetector

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val textView = TextView(this).apply {
            text = "Touch me"
            textSize = 24f
            gravity = Gravity.CENTER
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        }
        setContentView(textView)

        gestureDetector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
            override fun onSingleTapUp(e: MotionEvent?): Boolean {
                Toast.makeText(this@MainActivity, "Tapped!", Toast.LENGTH_SHORT).show()
                return true
            }

            override fun onLongPress(e: MotionEvent?) {
                Toast.makeText(this@MainActivity, "Long Pressed!", Toast.LENGTH_SHORT).show()
            }

            override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
                if (velocityX > 1000) {
                    Toast.makeText(this@MainActivity, "Swiped Right!", Toast.LENGTH_SHORT).show()
                } else if (velocityX < -1000) {
                    Toast.makeText(this@MainActivity, "Swiped Left!", Toast.LENGTH_SHORT).show()
                }
                return true
            }
        })

        textView.setOnTouchListener { _, event ->
            gestureDetector.onTouchEvent(event)
            true
        }
    }
}
OutputSuccess
Important Notes

Always return true from gesture methods to indicate the event was handled.

Use setOnTouchListener on the view you want to detect gestures on.

GestureDetector simplifies detecting common gestures without manual calculations.

Summary

Gesture handling lets apps respond to user touches like taps, swipes, and long presses.

Use GestureDetector with SimpleOnGestureListener to detect gestures easily.

Attach a touch listener to your view and pass events to GestureDetector.