Android - Gestures Tutorial


Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures.

Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not. To use it , you need to create an object of GestureDetector and then extend another class with GestureDetector.SimpleOnGestureListener to act as a listener and override some methods. Its syntax is given below −

GestureDetector myG;
myG = new GestureDetector(this,new Gesture());
   class Gesture extends GestureDetector.SimpleOnGestureListener{
      public boolean onSingleTapUp(MotionEvent ev) {
   public void onLongPress(MotionEvent ev) {
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {

Handling Pinch Gesture

Android provides ScaleGestureDetector class to handle gestures like pinch e.t.c. In order to use it, you need to instantiate an object of this class. Its syntax is as follow −

ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());

The first parameter is the context and the second parameter is the event listener. We have to define the event listener and override a function OnTouchEvent to make it working. Its syntax is given below −

public boolean onTouchEvent(MotionEvent ev) {
   return true;

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
   public boolean onScale(ScaleGestureDetector detector) {
      float scale = detector.getScaleFactor();
      return true;

Apart from the pinch gestures , there are other methods available that notify more about touch events. They are listed below −

Sr.No Method & description
1 getEventTime()

This method get the event time of the current event being processed..

2 getFocusX()

This method get the X coordinate of the current gesture's focal point.

3 getFocusY()

This method get the Y coordinate of the current gesture's focal point.

4 getTimeDelta()

This method return the time difference in milliseconds between the previous accepted scaling event and the current scaling event.

5 isInProgress()

This method returns true if a scale gesture is in progress..

6 onTouchEvent(MotionEvent event)

This method accepts MotionEvents and dispatches events when appropriate.


Here is an example demonstrating the use of ScaleGestureDetector class. It creates a basic application that allows you to zoom in and out through pinch.

To experiment with this example , you can run this on an actual device or in an emulator with touch screen enabled.

Steps Description
1 You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication;. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2 Modify src/ file to add necessary code.
3 Modify the res/layout/activity_main to add respective XML components
4 Run the application and choose a running android device and install the application on it and verify the results

Following is the content of the modified main activity file src/

package com.example.sairamkrishna.myapplication;

import android.content.Intent;
import android.content.IntentFilter;


import android.os.BatteryManager;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends Activity {
   private ImageView iv;
   private Matrix matrix = new Matrix();
   private float scale = 1f;
   private ScaleGestureDetector SGD;
   protected void onCreate(Bundle savedInstanceState) {
      SGD = new ScaleGestureDetector(this,new ScaleListener());
   public boolean onTouchEvent(MotionEvent ev) {
      return true;
   private class ScaleListener extends ScaleGestureDetector.
   SimpleOnScaleGestureListener {
      public boolean onScale(ScaleGestureDetector detector) {
         scale *= detector.getScaleFactor();
         scale = Math.max(0.1f, Math.min(scale, 5.0f));
         matrix.setScale(scale, scale);
         return true;
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(, menu);
      return true;
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      //noinspection SimplifiableIfStatement
      if (id == {
         return true;
      return super.onOptionsItemSelected(item);

Following is the modified content of the xml res/layout/activity_main.xml.

<RelativeLayout xmlns:android=""
   xmlns:tools="" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
   <TextView android:text="Gestures  Example" android:layout_width="wrap_content"
      android:layout_centerHorizontal="true" />
      android:text="Tutorials point"
      android:textSize="35dp" />
      android:layout_alignParentEnd="true" />

Following is the content of the res/values/string.xml.

   <string name="app_name>My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>

Following is the content of AndroidManifest.xml file.

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

      android:theme="@style/AppTheme" >
         android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Before starting your application, Android studio will display following window to select an option where you want to run your Android application.

Choose Device

Select your mobile device as an option and then check your mobile device which will display your default screen −

Android Gestures Tutorial

Now just place two fingers over android screen , and separate them a part and you will see that the android image is zooming. It is shown in the image below −

Android Gestures Tutorial

Now again place two fingers over android screen, and try to close them and you will see that the android image is now shrinking. It is shown in the image below −

Android Gestures Tutorial