Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to work with Swipe Gestures in Android

In this article, we will study “Gestures” in android, which is a unique feature through which the user can perform various touch events like scroll, tap, fling, double tap, long press etc.

Objectives:

  1. To provide knowledge of various touch events user can perform.
  2. To familiarize the readers with GestureDetector class used to detect type of gesture performed on screen.
  3. To provide knowledge of Listener and its abstract methods.
Gesture: Gestures in android is defined as the movements of your fingers on android device screen and respond accordingly if there is any listener is implemented for gesture detection.

In this article, we will cover following gestures:

  • Scroll
  • Fling
  • Single Tap
  • Long press

Scroll Gesture: Scroll is a type of gesture in which the users press the screen and then hold and drag horizontally.

Fling Gesture: Fling is a type of gesture in which the user drags on the screen with a velocity greater than Threshold velocity (a user specified velocity).

Single tap Gesture: It is a type of gesture in which user press and release anywhere on the screen instantaneously.

Long Press Gesture: It is a type of gesture in which the user press and hold any point of the screen for some duration.

Classes and interfaces Used:

For performing gestures in Android, Gesture detector class and Listener are used. These are explained as follow:

GestureDetector class:

GestureDetector class is used to identify any type of touch event performed on the screen of android device and then perform any event if there is a listener with method defined for that gesture.

Instantiating GestureDetector class:

GestureDetector anyObject = new GestureDetector(getApplicationContext());

MotionEvent:

It is a class that holds all the details of gesture or touch event performed by the user like point where the screen is touched, pressure exerted by user, duration for what user holds, etc.

OnGestureListener:

OnGestureListener is defined as a listener that listens to the touch events or gestures referenced by GestureDetector class by converting Activity touch event into GestureDetector touch event by using overridden method onTouchMode of Activity.

OnGestureListener is implemented by the Activity class as given below:

class ActivityName extends Activity implements OnGestureListener
{
}

The OnGestureListener has six abstract method and all the methods must be defined by the activity which implements it.The abstract methods of OnGestureListener are explained as below:

OnDown: onDown method is called when the user press the screen even for short duration. This method is always called as the user always press screen while performing any type of gesture.

public Boolean onDown(MotionEvent e)
{
return booleanvalue;
}

Explanation:

This method has a return type Boolean, i.e. , it must returns either true or false value.

OnFling: onFling method is called when the user swipe on the screen.

public Boolean onDown(MotionEvent e1, MotionEvent e2, float velocitX, float veloctiyY)
{
return booleanvalue;
}

Explanation:

This method has a return type Boolean, i.e. , it must returns either true or false value.

It has four arguments ; first argument holds details of point where down gesture performed, second argument holds details of point where the swiping ends, third arguments represent velocity of fling along X-axis of screen and fourth arguments represent velocity of fling along Y-axis of screen .

OnScroll: onSroll method is called when the user presses the screen and then moves it without releasing pressure on the screen.

public Boolean onScroll(MotionEvent e1, MotionEvent e2, float velocitX, float veloctiyY)
{
return booleanvalue;
}

Explanation:

This method has a return type Boolean, i.e. , it must returns either true or false value.

It has four arguments ; first argument holds details of point where down gesture performed, second argument holds details of point where the scrolling ends, third arguments represent velocity of scroll along X-axis of screen and fourth arguments represent velocity of scroll along Y-axis of screen .

OnSingleTapUp: This method is called when the user pressed and released the screen instantaneously.

public Boolean onSingleTapUp(MotionEvent e)
{
return booleanvalue;
}

Explanation:

This method has a return type Boolean, i.e. , it must returns either true or false value.

OnLongPress: This method is called when the user pressed the screen for some time. This method is triggered after 2000 milliseconds from initial down event.

public Boolean onLongPress(MotionEvent e)
{
return booleanvalue;
}

Explanation:

This method has a return type Boolean, i.e. , it must returns either true or false value.

OnShowPress: This method is called when the user pressed the screen and has not release the screen or not perform any other gesture since then.

public void onShowPressed(MotionEvent e)
{	
}

Explaination:

This method has a return type void, i.e., it returns nothing.

onTouchEvent Method: This is a overridden method of extended Activity class that must returns the onTouchEvent of GestureDetector class.

Public Boolean onTouchEvent(MotionEvent e)
{
	Return GestureDetectorObject.onTouchEvent(e);}

Steps Involved in making project on Gestures:

Step 1

Create a new project named Gesture in workspace with package name com.nkm.gesture.

Note: You can also choose project and package name according to your wish but make sure to modify the code accordingly.

Step 2

Make the java class file and name it as GestureActivity in the com.nkm.gesture package if not found in the package .

Step 3

Listing 1: Copy the following code in the java file

package com.nkm.gesture;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

public class GestureActivity extends Activity implements OnGestureListener{
    TextView textview;
    GestureDetector detector;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textview = new TextView(this);
        textview.setText("Gesture Demo");
        detector=new GestureDetector(this);
        setContentView(textview);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	return detector.onTouchEvent(event);
    }
	@Override
	public boolean onDown(MotionEvent e) {
		Toast.makeText(getApplicationContext(), "OnDown Gesture", 100).show();
		return false;
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		Toast.makeText(getApplicationContext(), "Fling Gesture", 100).show();
		return true;
	}
	@Override
	public void onLongPress(MotionEvent e) {
		Toast.makeText(getApplicationContext(), "Long Press Gesture", 100).show();
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		Toast.makeText(getApplicationContext(), "Scroll Gesture", 100).show();
		return false;
	}
	@Override
	public void onShowPress(MotionEvent e) {
		Toast.makeText(getApplicationContext(), "Show Press gesture", 100).show();
	}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		Toast.makeText(getApplicationContext(), "Single Tap Gesture", 100).show();
		return true;
	}
}

Explanation:

public class GestureActivity extends Activity implements Listener{

GestureActivity is the class name that extends Activity class and implements Listener interface to work with various gestures.

public boolean onTouchEvent(MotionEvent event) {
           return detector.onTouchEvent(event);
}

This is overridden method of Activity class to make touch events to be return as Gestures that are listen by OnGestureListener.

public boolean onDown(MotionEvent e) {
	Toast.makeText(getApplicationContext(), "OnDown Gesture", 100).show();
	return false;
}

This is an abstract method of Listener interface which is triggered when the user touch the screen and will display onDown Gesture through toast.

This method has a return false, i.e., the body of the method will not be executed when the gesture is performed.

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
	Toast.makeText(getApplicationContext(), "Fling Gesture", 100).show();
	return true;
}

This is an abstract method of Listener interface which is triggered when the user swipe on the screen and will display Fling Gesture through toast.

This method has a return false, i.e., the body of the method will not be executed when the gesture is performed.

public void onLongPress(MotionEvent e) {
	Toast.makeText(getApplicationContext(), "Long Press Gesture", 100).show();
}

This is an abstract method of Listener interface which is triggered when the user press anywhere on the screen for a duration of more than 2000 milliseconds and will display Long Press Gesture through toast.

This method returns nothing as it’ s return type is void.

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
	Toast.makeText(getApplicationContext(), "Scroll Gesture", 100).show();
	return false;
}

This is an abstract method of Listener interface which is triggered when the user press and scroll on the screen and will display Scroll Gesture through toast.

public void onShowPress(MotionEvent e) {
	Toast.makeText(getApplicationContext(), "Show Press gesture", 100).show();
}

This is an abstract method of Listener interface which is triggered when the user pressed the screen and does not perform any other gesture or release the screen. It will display Show Press Gesture through toast.

This method returns nothing as it’sits return type is void.

public boolean onSingleTapUp(MotionEvent e) {
	Toast.makeText(getApplicationContext(), "Single Tap Gesture", 100).show();	
	return true;
}

This is an abstract method of Listener interface which is triggered when the user press and release the screen instantaneously and it will display Single Tap Gesture through toast.

This Method performs the gesture operation and executes method body as it returns true.

Screen Shots:

Ondown gesture performed

Figure 1: Ondown gesture performed

Fling gesture performed

Figure 2: Fling gesture performed

Single Tap gesture performed

Figure 3: Single Tap gesture performed

Conclusion

In this article, we learned the below points:

  • Familiarization with various gestures.
  • Introduction to GestureDetector class.
  • Understanding of Listener and its abstract methods.


Working in Software Development domain from 7 years now and is well equipped with programming languages like HTML, CSS, Java, PHP, .NET etc.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login