How To Detect Gestures in Android

There are different types of touch events in Android. These touch events make the different type of gestures.Touch gestures occur when user place one or two fingers on the screen. The application collects the data of touch events and then relate it to a particular gesture. We will create a sample application project to detect the most common gestures available in Android.

How to Detect Gestures in Android – Implementation

Create a new project in Android Studio with default settings. We have only one activity in our application. Add the below code in the MainActivity java file.

MainActivity.java

GestureDedector class is used to detect common gestures. We will override the onTouchEvent() method in order to intercept the touch events. The process of collecting gestures start as soon as a user touches the screen with one finger or two fingers and it ends with the final action of user’s finger. All these events are passed to the instance of GestureDedector.

Now we will create a basic layout for our application. Add a TextView in the main layout file. We are going to update the TextView on different gestures.

main_activity.xml


download code

Run the application in the emulator. Below is the output of the application.

detecting gestures android

Video Demo

In this video, you could see the different gestures detected by the application.

I hope you will like this tutorial. Subscribe to our Newsletter to get new articles and tutorials in your inbox as soon as they published. You can connect with us on Facebook and also subscribe to our Youtube Channel.

Check Also

asynctask kotlin example

Kotlin AsyncTask Tutorial with Example

Here is an example of implementing AsyncTask in Kotlin. I will create an Android application …

  • Syed Kamran

    Nice tutorial…