Simple Gesture – Fling

person Jason Huangfolder_openAndroid, Codelocal_offer, , , access_time November 17, 2010

In Android, it’s very easy to implement a simple gesture such as Fling (the action of your finger wipe across the screen in a straight line).

for the Event onFling, if you don’t have code to handle finer gesture detection, such as taking into account x,y and velocity, Fling would work¬†whether¬†you swipe up and down / down and up/ left to right / right to left.

In the following example, I will show you how to easily implement a fling gesture to your app (without using Gesture view control), to simply open another screen. (Calling another activity):


1. You need to import the following library to your code:

//gesture
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

2. Your class needs to implement the OnGestureListner:

public class Tipster extends Activity implements OnClickListener, OnGestureListener{}

3. Create a private variable GestureDetector in your class variable definition:

private GestureDetector myGesture ;

4. On the class’ onCreate, initialize private GestureDetector:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        myGesture = new GestureDetector(this);

5. The most important part, is to add onTouchEvent to your class:

    @Override
    public boolean onTouchEvent(MotionEvent event){
    	return myGesture.onTouchEvent(event);
    }

6. by implementing the class “OnGestureListener”, you will need to implement the following functions:
To open another screen (activity), just write code inside the onFling function. For example, my onFling calls viewHistory function which opens the History screen.

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
 
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		ViewHistory();
		return false;
	}
 
	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
 
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
    	return false;
    }

To have finer control, such as only do something when swiping from right to left try this:

//these constants are used for onFling
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
 
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
 
        try {
            //do not do anything if the swipe does not reach a certain length of distance
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
 
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
 
            }
            // left to right swipe
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            	ViewHistory();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
 
	}

Comments

  1. Thanks! Very helpful and works great.

  2. Fantastic! Step-by-step description, with code-snippet, perfect for beginners. Thank you.

  3. thank you very much for this post

  4. Thank you very much for sharing. Great post

  5. waste of time not workng

  6. yes u have proved that this code is done by monkey… because it is some how it is working…

  7. Easy, nice. Just add context for the GestureDetector as it become depreciate now without it.

  8. Chirayu Modi

    Awesome work… Thanks a lot buddy

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>