Knowpapa.com - a developer's blog

Android Gesture Detector Demo

Android provides a dedicated GestureDetector class to detect common gestures. It implements a SimpleOnGestureListener which can be used to detect the following gestures:

onDoubleTap(MotionEvent e)
onDoubleTapEvent(MotionEvent e)
onDown(MotionEvent e)
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
onLongPress(MotionEvent e)
onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
onShowPress(MotionEvent e)
onSingleTapConfirmed(MotionEvent e)
onSingleTapUp(MotionEvent e)

Let us see an example that implements a simple gesture detector.

The following Activity first instantiates a new GestureDetector via a call to setupGestureDetector()
The program then simply delegates the task of gesture detection to setupGestureDetector() via the onTouchEvent method.

Also note that most of the methods have a boolean return type, where a return of true means that the event has been consumed by the given method and it wont propagate further. A false return value indicates that the event was not consumed by the method and other listeners above it in hierarchy may consume the event.

The code is:

package com.knowpapa.gesturedetectordemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class MainActivity extends Activity {

	protected static final String TAG = "GestureDetectorMainActivity";
	private GestureDetector mGestureDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setupGestureDetector();
	}

	private void setupGestureDetector() {

		mGestureDetector = new GestureDetector(this,

		new GestureDetector.SimpleOnGestureListener() {
			@Override
			public boolean onDoubleTap(MotionEvent e) {
				Log.i(TAG, "onDoubleTap");
				return true;
			}

			@Override
			public boolean onDoubleTapEvent(MotionEvent e) {
				Log.i(TAG, "onDoubleTapEvent");
				return true;
			}

			@Override
			public boolean onDown(MotionEvent e) {
				Log.i(TAG, "onDown");
				return true;
			}

			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				Log.i(TAG, "onFling");
				return true;
			}

			@Override
			public void onLongPress(MotionEvent e) {
				Log.i(TAG, "onLongPress");
			}

			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2,
					float distanceX, float distanceY) {
				Log.i(TAG, "onScroll");
				return true;
			}

			@Override
			public void onShowPress(MotionEvent e) {
				Log.i(TAG, "onShowPress");
			}

			@Override
			public boolean onSingleTapConfirmed(MotionEvent e) {
				Log.i(TAG, "onSingleTapConfirmed");
				return true;
			}

			@Override
			public boolean onSingleTapUp(MotionEvent e) {
				Log.i(TAG, "onSingleTapUp");
				return true;
			}
		});
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mGestureDetector != null) {
			return mGestureDetector.onTouchEvent(event);
		} else {
			return super.onTouchEvent(event);
		}

	}

You can simply run this program and try some touch gestures on the screen and see it being logged into the log.

Additionally the MotionEvent parameter in various gesture methods can be used to get a range of information like the location coordinates, velocity and several other details for the touch event. Details of MotionEvent parameters can be found here.