Home > DeveloperSection > Articles > Gesture in Android

Gesture in Android

Android Android  API(s) 
0 Comment(s)
 1023  View(s)
Rate this:

Android Gesture

A "touch 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. There are correspondingly two phases to 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, 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 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 of the onTouchEvent() callback method 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"











    tools:context=".MainActivity" >








        android:src="@android:drawable/sym_def_app_icon" />




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;


                protected void onCreate(Bundle savedInstanceState) {



                                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());




                public boolean onTouchEvent(MotionEvent ev) {


                                return true;


                private class ScaleListener extends

                                                ScaleGestureDetector.SimpleOnScaleGestureListener {


                                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);


                                                return true;





Now run your application 

Zoom image



Don't want to miss updates? Please click the below button!

Follow MindStick