Using Gestures in ANDROID,A Simple example.

By | March 29, 2011

This is a really simple example illustrating gestures in ANDROID.
You Know that everything you do with your hand inside the phone is a gesture like SingleTap, doubleTap etc.
Here is a quick illustration of this.
For this the activity must implement OnGestureListener. The GestureDetector detects the gestures.
Let’s look at the example.

package pack.GestureSampleThree;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GestureSampleThreeExample extends Activity implements OnGestureListener {
	 private LinearLayout main;
	    private TextView viewA;

	    private GestureDetector gestureScanner;

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        gestureScanner = new GestureDetector(this);

	        main = new LinearLayout(this);
	        main.setBackgroundColor(Color.GRAY);
	        main.setLayoutParams(new LinearLayout.LayoutParams(320,480));

	        viewA = new TextView(this);
	        viewA.setBackgroundColor(Color.YELLOW);
	        viewA.setTextColor(Color.BLACK);
	        viewA.setTextSize(16);
	        viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
	        main.addView(viewA);

	        setContentView(main);
	    }

	    @Override
	    public boolean onTouchEvent(MotionEvent me) {
	        return gestureScanner.onTouchEvent(me);
	    }


	    public boolean onDown(MotionEvent e) {
	        viewA.setText("-" + "DOWN" + "-");
	        return true;
	    }

	    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	        viewA.setText("-" + "FLING" + "-");
	        return true;
	    }


	    public void onLongPress(MotionEvent e) {
	        viewA.setText("-" + "LONG PRESS" + "-");
	    }


	    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
	        viewA.setText("-" + "SCROLL" + "-");
	        return true;
	    }


	    public void onShowPress(MotionEvent e) {
	        viewA.setText("-" + "SHOW PRESS" + "-");
	    }


	    public boolean onSingleTapUp(MotionEvent e) {
	        viewA.setText("-" + "SINGLE TAP UP" + "-");
	        return true;
	    }
	}

The layout file main.xml for the above code is

<?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="@string/hello"
    />
</linearLayout>

The manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.GestureSampleThree"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GestureSampleThreeExample"
                  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>

A complete example for making your own gesture application in ANDROID is explained in this tutorial

One thought on “Using Gestures in ANDROID,A Simple example.

  1. Pingback: Finger swipe in Android application - Programmers Goodies

Leave a Reply

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