Android Detect Gestures Tutorial

Gesture occurs when a user places one or more fingers on the touch screen, and your application interprets that pattern of touches as a particular gesture. Android provides special types of gestures such as pinch, double tap, scrolls, long presses and flinch. Android provides GestureDetector class to receive gestures events and tell us that these events occur in the smart phone. The purpose of the gesturedetector class is to Gathering data about touch events and Interpreting the data to see if it meets the criteria for any of the gestures your app supports. In this tutorial, I will tell you how to use android detect gestures in your new project.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “GesturesExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit activity_main.xml layout

Add the textview inside the constraint-layout, add a new id for textview “txt_gesture” so that you can display the gesture status in the UI.

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />


Edit class

Go to class and implements GestureDectector listeners, After that initialize all callbacks from the listeners. You can follow the sample below.

package com.questdot.gesturesexample;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,

    private TextView txt_gesture;
    private GestureDetectorCompat gestureDetectorCompat;

    protected void onCreate(Bundle savedInstanceState) {

        txt_gesture = (TextView)findViewById(;

       gestureDetectorCompat = new GestureDetectorCompat(getApplicationContext(),this);


    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {

        txt_gesture.setText("On Single tap");
        return true;

    public boolean onDoubleTap(MotionEvent motionEvent) {
        txt_gesture.setText("On Double tap");
        return true;

    public boolean onDoubleTapEvent(MotionEvent motionEvent) {
        txt_gesture.setText("On Double tap event tap");
        return true;

    public boolean onDown(MotionEvent motionEvent) {
        txt_gesture.setText("On Down ");
        return true;

    public void onShowPress(MotionEvent motionEvent) {
        txt_gesture.setText("On Show ");


    public boolean onSingleTapUp(MotionEvent motionEvent) {
        txt_gesture.setText("On Single tap up");
        return true;

    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        txt_gesture.setText("On Scroll ");
        return true;

    public void onLongPress(MotionEvent motionEvent) {
        txt_gesture.setText("On Long Press");


    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        txt_gesture.setText("On Fling");
        return true;

    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);

Run Your Project

In conclusion, now you can run this project in the device and test out all the gestures listener that you implement.

(Android Detect Gestures)

Source Code

(Visited 478 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *