---
title: "Gesture in Android"  
description: "In this article I am explaining about Gesture in Android"  
author: "Manoj Pandey"  
published: 2015-03-11  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1649/gesture-in-android  
category: "android"  
tags: ["android", "api(s)"]  
reading_time: 3 minutes  

---

# Gesture in Android

A "touch gesture" occurs when a user places one or more fingers on the [touch screen](https://answers.mindstick.com/qa/102415/explain-the-functions-of-a-computer-touch-screen), and [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding) interprets that pattern of touches as a particular gesture. There are correspondingly two phases to [gesture detection](https://www.mindstick.com/interview/2548/android-basic-gesture-detection):

· Gathering data about touch events.

· Interpreting the data to see if it meets the criteria for any of the gestures your app supports.

When a user interacts with the display of an [Android device](https://www.mindstick.com/articles/342298/best-ways-to-customize-your-android-device-for-better-productivity), the onTouchEvent() method of the currently active application is called by the system and passed MotionEvent objects containing data about the user’s contact with the screen. This data can be interpreted to identify if the motion on the screen matches a common gesture such as a tap or a swipe. This can be achieved with very little [programming](https://yourviews.mindstick.com/view/88478/vibe-coding-the-rise-of-ai-assisted-programming) effort by making use of the Android GestureDetectorCompat class. This class is designed specifically to receive motion event information from the application and to trigger method calls based on the type of common gesture, if any, detected.

##### The basic steps in detecting common gestures are as follows:

1. Declaration of a class which implements the GestureDetector.OnGestureListener interface including the required onFling(), onDown(), onScroll(), onShowPress, onSingleTapUp() and onLongPress() callback methods. Note that this can be either an entirely new class, or the enclosing activity class. In the event that double tap gesture detection is required, the class must also implement the GestureDetector.OnDoubleTapListener interface and include the corresponding onDoubleTap() method.

2. Creation of an instance of the Android GestureDetectorCompat class, passing through an instance of the class created in step 1 as an argument.

3. An optional call to the setOnDoubleTapListener() method of the GestureDetectorCompat instance to enable double tap detection if required.

4. [Implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) of the onTouchEvent() [callback method](https://www.mindstick.com/forum/215/callback-method-in-jquery) on the enclosing activity which, in turn, must call the onTouchEvent() method of the GestureDetectorCompat instance, passing through the current motion event object as an argument to the method.

##### Here I am creating sample of Image Zooming via use gesture

1. Create an android project

2. Add image in layout

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_gravity="center"    android:gravity="center"    android:orientation="horizontal"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >     <ImageView        android:padding="80sp"        android:id="@+id/imageView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="matrix"        android:src="@android:drawable/sym_def_app_icon" /> </LinearLayout>
```

3. Add following code in MainActivity.class

```
package com.example.androidgesture;import android.app.Activity;import android.graphics.Matrix;import android.os.Bundle;import android.view.Menu;import android.view.MotionEvent;import android.view.ScaleGestureDetector;import android.widget.ImageView;public class MainActivity extends Activity {                private ImageView img;                private Matrix matrix = new Matrix();                private float scale = 1f;                private ScaleGestureDetector SGD;                @Override                protected void onCreate(Bundle savedInstanceState) {                                super.onCreate(savedInstanceState);                                setContentView(R.layout.activity_main);                                img = (ImageView) findViewById(R.id.imageView1);                                // A convenience class to extend when you only want to listen for a                                // subset of scaling-related events.                                SGD = new ScaleGestureDetector(this, new ScaleListener());                }                 @Override                public boolean onTouchEvent(MotionEvent ev) {                                SGD.onTouchEvent(ev);                                return true;                }                private class ScaleListener extends                                                ScaleGestureDetector.SimpleOnScaleGestureListener {                                @Override                                public boolean onScale(ScaleGestureDetector detector) {                                                // Return the scaling factor from the previous scale event to the                                                // current event.                                                scale *= detector.getScaleFactor();                                                scale = Math.max(0.1f, Math.min(scale, 5.0f));                                                matrix.setScale(scale, scale);                                                img.setImageMatrix(matrix);                                                return true;                                }                }} 
```

Now run your application

**![Gesture in Android](https://www.mindstick.com/mindstickarticle/c31ea7a6-139c-402a-a736-e1d608b67498/images/adb8c8f4-81f8-4f88-a6fb-032f9c5655ff.png)**

Zoom image

**![Gesture in Android](https://www.mindstick.com/mindstickarticle/c31ea7a6-139c-402a-a736-e1d608b67498/images/edd76e18-0691-4efe-bcc2-012c94f151f6.png)**

---

Original Source: https://www.mindstick.com/articles/1649/gesture-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
