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.

OnTouch

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() {
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		yourGestureListener.onTouchEvent(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 :

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Android Tutorial: Gestures in your app"

  1. Rick Hathaway says:

    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!

Leave a Reply


five + 9 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close