Monday, June 18, 2012

How To Implement Gestures In Your Android Application

The project describes how to implement gestures for your application.

Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project GestureExample
2.) Insert following code in your main.xml:
<LinearLayout xmlns:android=""

   android:text="Try to draw the gesture"
   android:layout_weight="1.0" />
3.) Copy the gesture library from the SD card – e.g. by opening the Windows-> Show View -> Android File Explorer in Eclipse and save it somewhere for now. Now Create directory res/raw and put the downloaded gesture library “gesture” in there
4.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. GestureExample.
2.) Enter following information:
Project name: GestureExample
Build Target: Android 2.3.3
Application name: GestureExample
Package name: org.example. GestureExample
Create Activity: GestureExample
On Clicking Finish GestureExample code structure is generated with the necessary Android Packages being imported along with GestureExample class will look like following:
package org.example.GestureExample;
import android.os.Bundle;
import java.util.ArrayList;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.util.Log;
import android.widget.Toast;

 * A simple Activity listening for Gestures

public class GestureExample extends Activity {
        private GestureLibrary gLib;
        private static final String TAG = "";

        public void onCreate(Bundle savedInstanceState) {

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

                GestureOverlayView gestures = (GestureOverlayView)findViewById(;

         * our gesture listener

        private OnGesturePerformedListener handleGestureListener = newOnGesturePerformedListener() {
                public void onGesturePerformed(GestureOverlayView gestureView,
                                Gesture gesture) {

                        ArrayList<Prediction> predictions =gLib.recognize(gesture);

                        // one prediction needed
                        if (predictions.size() > 0) {
                                Prediction prediction = predictions.get(0);
                                // checking prediction
                                if (prediction.score > 1.0) {
                                        // and action
Output – The final output

No comments:

Post a Comment