Open In App

How to Detect Swipe Direction in Android?

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Detecting gestures is a very important feature that many app developers focus on. There can be a number of gestures that could be required to perform certain actions. For example, a user might need to swipe the screen from left to right to unlock the screen. Similarly, vice-versa might be needed. In such cases, it is necessary to detect the direction of the swipe or gesture made by the user. Similarly, most gaming applications depend heavily on user gestures to perform desired actions.

So through this article, we will show you how you could detect the swipe direction of the user input on the screen.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Note: Select Kotlin as the programming language.

Step 2: Add this in the Main code (MainActivity.kt)

Refer to the comments inside the code for better understanding.

MainActivity.kt:

Kotlin
package org.geeksforgeeks.demo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.Toast
import kotlin.math.abs

class MainActivity : AppCompatActivity(), GestureDetector.OnGestureListener {

    // Declaring gesture detector, swipe threshold, and swipe velocity threshold
    private lateinit var gestureDetector: GestureDetector
    private val swipeThreshold = 100
    private val swipeVelocityThreshold = 100

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initializing the gesture detector
        gestureDetector = GestureDetector(this, this)
    }

    // Override this method to recognize touch event
    override fun onTouchEvent(event: MotionEvent): Boolean {
        return if (gestureDetector.onTouchEvent(event)) {
            true
        }
        else {
            super.onTouchEvent(event)
        }
    }

    // All the below methods are GestureDetector.OnGestureListener members
    // Except onFling, all must "return false" if Boolean return type
    // and "return" if no return type
    override fun onDown(e: MotionEvent): Boolean {
        return false
    }

    override fun onShowPress(e: MotionEvent) {
        return
    }

    override fun onSingleTapUp(e: MotionEvent): Boolean {
        return false
    }

    override fun onScroll(
        e1: MotionEvent?,
        e2: MotionEvent,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        return false
    }

    override fun onLongPress(e: MotionEvent) {
        return
    }

    // This method is called when a swipe gesture is detected
    override fun onFling(
        e1: MotionEvent?,
        e2: MotionEvent,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        try {
            val diffY = e2.y - e1!!.y
            val diffX = e2.x - e1.x

            if (abs(diffX) > abs(diffY)) {
                
                // Horizontal swipe
                if (abs(diffX) > swipeThreshold && abs(velocityX) > swipeVelocityThreshold) {
                    if (diffX > 0) {
                        showToast("Swipe: Left -> Right")
                    } else {
                        showToast("Swipe: Right -> Left")
                    }
                    return true
                }
            } else {
                
                // Vertical swipe
                if (abs(diffY) > swipeThreshold && abs(velocityY) > swipeVelocityThreshold) {
                    if (diffY > 0) {
                        showToast("Swipe: Top -> Bottom")
                    } else {
                        showToast("Swipe: Bottom -> Top")
                    }
                    return true
                }
            }
        } catch (exception: Exception) {
            exception.printStackTrace()
        }
        return false
    }

    // Define a method to show a toast message
    private fun showToast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}


Output:




Next Article

Similar Reads