Android Core

Android Tutorial: Gestures in your app

Gestures in mobile apps are pretty common place these days. Almost anyone with smart phone experience knows that pinching will usually zoom in on an image. Now, using Gestures in Android is a snap.


All gestures are really handled with one event on an Activity. OnTouch is called anytime a View is touched on the screen. It provides all the information you’ll need to create some pretty interesting gestures. Android provides a few simple classes that allow you to quickly add some gestures to your application without really getting into the details of gestures. When OnTouch is fired, you receive a MotionEvent and a reference to the View that was touched.

Explanation of a Motion Event

The Motion Event is what the Android OS returns every time the screen is touched in the OnTouch callback. The MotionEvent object contains information about how many fingers are touching the screen and the velocity of a finger that is moving. It has all the details needed to handle any kind of Gesture. Android also goes a step further.

Android provides a few nice classes for some basic gesture detection, such as single finger drag and drop. They provide the developer with an easy way to implement a few gestures without really getting your hands dirty by using the SimpleOnGestureListener.

How to use the OnGestureListener

Using the OnGestureListener is very easy. In your activity, hook up your OnTouch Listener to the root view of your activity (if you’re looking to have gestures on the full screen).

rootLayout.setOnTouchListener(new OnTouchListener() {
	public boolean onTouch(View v, MotionEvent event) {
		return false;

Now all of the touch events on the rootlayout will be handled by your SimpleOnGestureListener. All that is left is to implement what your Gesture Listener does on certain touch events. By simply overriding the methods you need, you can implement gesture functionality without determining what type of gesture the user has done. For example, you can override the OnScroll method of the SimpleOnGestureListener to implement your own function for scrolling on your View.

Why the Simple GestureDetector isn’t so great

The Simple GestureDetector is great for any gesture that only requires some basic gestures. However, for any gesture looking for two (or more) touches, you’re out of luck. If you really want to get into the thick of Gestures, you really need to look into the Motion Event object a bit further.

Creating More Complicated Gestures

Lets go in a different direction from the above example. Lets say, instead of just calling the Gesture Listener in our OnTouch callback, lets look at the Motion Event object a bit more.

Each Motion Event we receive has its own action, such as DOWN, where a finger has been pressed on the view, or MOVE, where a finger has moved from one position on the two dimensional plane to another. The Motion Event also has an action that is very useful for handling multi-touch gestures, called POINTER_DOWN, which is used when a second touch is placed on the view.

Using these actions, complex gestures can be created. For example, a pinch gesture (usually used to zoom in on a map) could work like this:

1. Received DOWN. We record the initial spot where the user touched the screen.

2. Received POINTER_DOWN. Since we know this could be a two finger gesture, we record the spot of the second touch

3. Received MOVE. Now we check to see if the two pointers we have detected have moved towards each other. *

*Note that within a Motion Event object, it is possible to get the coordinates of different touches by using the event.getX() method.

Gesture Predictions

Android also has a way to load in specific gestures to your application as well. These specific gestures can be more unique than the ones mentioned above, such as drawing a Z symbol on the screen.

Creating these types of gestures requires the Emulator. You first draw your gestures in the emulator, and then you save them into a file. You can then take your created gesture file and load it into your application. By adding a GestureOverLayView onto your Layout, you can use your newly-created gestures to do whatever you would like.

Click here to get some more information on Gesture Predictions.

Reference: Android Tutorial: Adding Gestures to Your App from our JCG partner Isaac Taylor at the Programming Mobile blog.

Related Articles :
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Rick Hathaway
Rick Hathaway
11 years ago

Anyone know if this is relavent to usage with Cocos2D? Just got into Android development about a year ago and still struggle some with the conventions of how it all fits together. Great article, by the way, easy and effective read!

8 years ago
Reply to  Rick Hathaway

please put screenshots please

Back to top button