Gestures in Android Using Android Studio

Introduction

 
This article explains how to create and detect your own gestures in Android.
 
Android shows the gestures in yellow for recognized gestures and a lighter yellow for gestures that are not registered. You can turn this off, via setGestureColor(Color.TRANSPARENT) or setUncertainGestureColor(Color.TRANSPARENT) on the GestureOverlayView.
 
Step 1
 
First, let us create some gestures.
 
Start your emulator. Open the "Gestures Builder" app in your emulator. You will see something like:
 
im1.jpg
 
At present in this emulator, no gestures have been added.
 
Add some gestures by clicking on "Add gesture".
 
im2.jpg
 
Enter a name for your gesture and draw the gesture in the remaining area and click on done. For example:
 
im3.jpg
 
I have added 3 gestures with names "happy", "sad", "surprised" as in the following:
 
im4.jpg
 
You can also rename the gesture added by holding the click/touch on the gesture name in the list shown above.
 
After creating the gestures, the most obvious step is to pull the gesture file and add it to your project.
 
Step 2
 
For pulling the gesture file from the emulator.
 
Open Android Debug Monitor (DDMS included) then select "File Explorer" -> "mnt" -> "sdcard" -> "gestures". Now click on "Pull a file from device" in the top right corner. Save this file anywhere on your computer. I have saved this with the name "expressions".
 
Step 3
 
Right-click on res then select "New" -> "Android Resource Directory". Name the directory "raw" and choose the type as "raw".
 
Copy the file saved in step 2 (expressions in this case), to the clipboard and paste it in the "raw" folder that you created.
 
Now you can use this file.
 
Step 4
 
Open "activity_main" and add the following code to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.               android:orientation="vertical"  
  3.               android:layout_width="fill_parent"  
  4.               android:layout_height="fill_parent"  
  5.               android:background="#ffc366"    >  
  6.     <TextView  
  7.             android:layout_width="fill_parent"  
  8.             android:layout_height="wrap_content"  
  9.             android:textSize="20dp"  
  10.             android:gravity="center_horizontal"  
  11.             android:text="Draw the expression"  
  12.             android:layout_margin="10dip"/>  
  13.    
  14.     <android.gesture.GestureOverlayView  
  15.             android:id="@+id/gesture_area"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="0dip"  
  18.             android:layout_weight="1.0"  
  19.             android:background="#454545"/>  
  20.    
  21. </LinearLayout> 
"GestureOverlayView" in the code above is the area where the user can draw their gestures,
 
The layout looks like:
 
im5.jpg
 
Step 5
 
Open "MainActivity" and add the following code to it:
  1. package com.chhavi.gestures;  
  2.    
  3. import android.gesture.Gesture;  
  4. import android.gesture.GestureLibraries;  
  5. import android.gesture.GestureLibrary;  
  6. import android.gesture.GestureOverlayView;  
  7. import android.gesture.Prediction;  
  8. import android.os.Bundle;  
  9. import android.app.Activity;  
  10. import android.view.Menu;  
  11. import android.widget.Toast;  
  12. import java.util.ArrayList;  
  13.    
  14. public class MainActivity extends Activity implements GestureOverlayView.OnGesturePerformedListener {  
  15.     private GestureLibrary gesLib;  
  16.    
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.    
  22.         gesLib = GestureLibraries.fromRawResource(this, R.raw.expressions);  
  23.         if (!gesLib.load()) {  
  24.             finish();  
  25.         }  
  26.    
  27.         GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gesture_area);  
  28.         overlay.addOnGesturePerformedListener(this);  
  29.     }  
  30.    
  31.     public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {  
  32.         ArrayList<Prediction> predictions = gesLib.recognize(gesture);  
  33.    
  34.          if (predictions.size() > 0) {  
  35.             Prediction prediction = predictions.get(0);  
  36.              
  37.             if (prediction.score > 1.0) {  
  38.                     Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();  
  39.             }  
  40.         }  
  41.     }  

In the code above, "GestureLibrary" is an abstract class.
 
"GestureLibraries.fromRawResource" gets the "expressions" file we placed in "raw" in step 3. This loads the gestures in this activity.
 
GestureOverlayView is a transparent overlay for gesture input that can be placed on top of other widgets or can contain other widgets.
 
"onGesturePerformed" is a method of the "OnGesturePerformedListener" interface. This will be called as soon as the gesture area encounters any gesture input.
Finally, the toast used above will display the name of the gesture drawn.
 
Output snapshots:
 
im6.jpg
 
Draw an expression:
 
im7.jpg
 
Toast recognizing the expression drawn:
 
im8.jpg
 
Another expression:
 
im9.jpg
 
Note that since this gesture is not defined, it will not be recognized. Also, it appears in light yellow color that shows that this gesture is not registered.
 
Thank you...


Similar Articles