Make your own gesture application in ANDROID or How to use gestures in ANDROID?

By | April 5, 2011

Hi all….
In this tutorial I will teach you how to make use of Gestures in ANDROID and make your own application.
Follow the below steps exactly to get an idea of how to use gestures.

1. Make a gesture Library.
2. Add your own gestures into it.
3. Export it to your applications and use it.

It is as simple as that…
Now let’s start………………..

1. Making a gesture library.

First start your emulator with the SD CARD .
Creating an SDCARD and starting the emulator with the SDCARD is explained in this
tutorial here then you have to download the whole project from the website here that
has the output as shown below in the screenshot.

Draw some of the gestures and save it. This is important.
Then only you can reproduce it in your own application.

Now go to file explorer and pull out the gestures file under the SDCARD from the
emulator and save it anywhere on your computer.

How to export a file from the SDCARD is covered in this tutorial

Now you have the gestures library, now you can use this in your own application.
Actually steps 2 and 3 are already covered.
Now we will make our own application and use the exported gesture library.
Next create another project and name the file GestureTestTwo.java and copy the following
code to it.

GestureTestTwo.java

package com.GestureTestTwo;

import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class GestureTestTwo extends Activity {
private GestureLibrary gLib;
private static final String TAG = "com.GestureTestTwo";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     gLib = GestureLibraries.fromRawResource(this,R.raw.gestures);
     if (!gLib.load()) {
          Log.w(TAG, "could not load gesture library");
       finish();
}

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(handleGestureListener);

}

/**
* our gesture listener
*/
private OnGesturePerformedListener handleGestureListener = new
OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView gestureView,
Gesture gesture) {
    ArrayList predictions = gLib.recognize(gesture);

     if (predictions.size() > 0) {
          Prediction prediction = predictions.get(0);
     if (prediction.score > 1.0) {
         Toast.makeText(GestureTestTwo.this, prediction.name,Toast.LENGTH_SHORT).show();
}
}
}
};
}

Make a folder named “raw” inside the “src” folder and drop the exported file into it.

After export the project will look like this

Main.xml for GestureTestTwo.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Try to draw the gesture"
    />
<android.gesture.GestureOverlayView
    android:id="@+id/gestures"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1.0" />
</LinearLayout>

Manifest file for GestureTestTwo.java

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.GestureTestTwo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GestureTestTwo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Now draw same gestures that you saved in the previous project.

The above is the one that I draw and saved in the previous project in the gesture library inside the
SDCARD. However make your own and test it. Above you can see the outputs.
If the gestures you draw matches then a toast with the gesture name will be shown

Please leave your comments if you need more help.

2 thoughts on “Make your own gesture application in ANDROID or How to use gestures in ANDROID?

  1. Pingback: Using Gestures in ANDROID,A Simple example. | Coderz Heaven

  2. Hanif Ullah Khan

    R.raw.gestures
    How we generate the gesture folder according to our requirements?
    Need a quick reply.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *