Posts Tagged ‘gesture builder’

Creating a simple Gesture Application in Android

January 10, 2012 18 comments

Android supports the Gesture API since version 1.6. The API can be located in the package android.gesture, and lets you store, load, draw and recognize gestures. This tutorial will show you a proof-of-concept application how you can make use of Gesture API.

We will define 2 gestures: “S” and “O”. When a corresponding gesture will be recognized, a toast message will be shown.

The result will look like in the screenshot below:

Create a new project:
Project: AndroidGesture
Activity: AndroidGestureActivity

1. Creating the gestures
Starting with version 1.6 and higher the Android Emulator includes a new application pre-installed, called Gestures Builder.

Start the Android Emulator and use the Gesture Builder application to create the “S” and “O” gestures:

Android Gesture

A gesture is always associated with a name, but the name does not necessarily have to be unique. In fact, it’s recommended to have several gestures with the same name to increase the precision of recognition.

2. Importing gesture to your project
Every time we create or edit gestures with Gesture Builder, a file is created on the emulator SD card: /sdcard/gestures. We should import this file into our /res/raw project directory.

In order to do this, open the FileExplorer tab in the DDMS perspective. (If you don’t have the FileExplorer tab available, add it from: Window -> Show View -> File Explorer). Navigate to /sdcard directory and copy the gesture file to your computer, for example on your desktop.

To copy the gesture file from the emulator, select it and click the “Pull a file from the device” button, marked with red in the screenshot below:

Android DDMS

Don’t forget to create a new folder called raw in the res directory of your project and copy there the gesture file.

3. Loading the gesture library and recognizing the gesture
To start recognizing gesture in our application we have to add the GestureOverlayView to our XML layout file. There are 2 ways you can use the GestureOverlayView, one of them is to use it as a normal view embedded inside a LinearLayout for example, and another is to use it as an overlay on top of other views. In this tutorial we will use the second option – an overlay on top of other views.

Edit the main.xml layout to look like this:

<?xml version="1.0" encoding="utf-8"?>
<android.gesture.GestureOverlayView xmlns:android=""
android:orientation="vertical" >

android:text="TextView" />


Make the AndroidGestureActivity to implement the OnGesturePerformedListener interface and add the the mLibrary member variable of type GestureLibrary:

public class AndroidGestureActivity extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;

In the onCreate() method we load the library and add the GestureOverlayView to the listener:

public void onCreate(Bundle savedInstanceState) {

   mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
   if (!mLibrary.load()) {

   GestureOverlayView gestures = (GestureOverlayView) findViewById(;

And below is the implementation of onGesturePerformed(). When the listener is triggered, a list of predictions and a score is returned, each with the name you entered earlier in the Gesture Builder. The list is sorted by descending scores; the higher the score, the more likely the associated gesture is the one the user intended to draw:

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
   ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

   if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
     String result = predictions.get(0).name;

     if ("open".equalsIgnoreCase(result)) {
       Toast.makeText(this, "Opening the document", Toast.LENGTH_LONG).show();
     } else if ("save".equalsIgnoreCase(result)) {
       Toast.makeText(this, "Saving the document", Toast.LENGTH_LONG).show();

By this time the application should compile and run successfully.



Get every new post delivered to your Inbox.

Join 484 other followers

%d bloggers like this: