VOOZH about

URL: https://www.geeksforgeeks.org/android/android-gestures-with-examples/

⇱ Android Gestures with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Android Gestures with Examples

Last Updated : 23 Jul, 2025

Android supports a range of touch gestures such as tap, double-tap, pinch, swipe, scroll, long press, drag, and fling. Drag and fling may seem similar but drag is the type of scrolling that occurs when a user drags their finger across the touchscreen, while a fling gesture occurs when the user drags and then lifts their finger quickly. A MotionEvent describes the state of touch event via an action code. A long list of action codes is supported by Android: 

  • ACTION_DOWN: A touch event has started.
  • ACTION_MOVE: A change that has occurred during the touch event (between ACTION_DOWN and ACTION_UP).
  • ACTION_UP: The touch event has finished. This contains the final release location.
  • ACTION_CANCEL: The gesture was canceled.

Note: You should perform same action during ACTION_CANCEL and ACTION_UP event.

Important Methods

  • getAction(): extract the action the user performed from the event parameter.

Important Interfaces

  • GestureDetector.OnGestureListener : notifies users when a particular touch event has occurred.
  • GestureDetector.OnDoubleTapListener : notifies users when a double-tap event has occurred.

Example 1

Let us see the way to perform some simple actions on events like ACTION_DOWN, ACTION_UP, etc.

Step 1: Create a New Project

To create a new project in Android Studio, refer to How to Create/Start a New Project in Android Studio. Select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Note: You may get an error on the following line:

import kotlinx.android.synthetic.main.activity_main.*

Please refer to this to fix this error.

Output:  

Explanation of Output: 

  • When we click on the screen or press on the screen, you can see in the video it says Action was DOWN 
  • When we move the mouse or the finger while pressing it, TextView says Action was Move.
  • When I release the mouse or lift my finger, it says Action was UP

Example 2 

Now let us see the way to perform some actions on events like single tap, double-tap, long press, etc. The below code uses the same activity_main.xml as used above.  

Working with the MainActivity.kt file: 

Output: 

Explanation of Output:  

  • When we single tap on the screen it says single tap and then single tap confirmed because the event was not canceled
  • When we press on the screen it says to press and if I keep pressing for longer it says the long press.
  • When we double-tap double-tap is shown in Text View as well as Toast.

Note: onTouchEvent() is for the activity but you can attach a View.OnTouchListener object to any View object using the setOnTouchListener() method.

By this method, you can perform actions when events are triggered inside a view because OnTouchListener is attached to that particular view. For example, for an ImageView with id "imp". 

img.setOnTouchListener { view, motionEvent ->

           // ... Respond to touch events

           true

}    

// for general view   

findViewById<View>(R.id.my_view).setOnTouchListener { v, event ->

            // ... Respond to touch events

           true

 }

Comment
Article Tags:
Article Tags:

Explore