[ANDROID] FlingGesture Listener


Today topic is about Fling Gesture in Android.

As you might notice that, some apps on Android like Music or Photo are implemented Fling gesture, so that if you fling you finger from left to right or from right to left, you will have your songs or images changed.

So, what was happened under the hook?

Let’s dive into these little snippet of code. (This is not my code, I just collect them from somewhere on the internet that I can not remember now, sorry for this, just I think you can try to Google them).


public class FlingGestureDetector extends SimpleOnGestureListener {
public static final int SWIPE_MIN_DISTANCE = 120;
public static final int SWIPE_MAX_OFF_PATH = 250;
public static final int SWIPE_THRESHOLD_VELOCITY = 200;
public onAction mOnL2RAction;
public onAction mOnR2LAction;

public FlingGestureDetector(Context context, View v){

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
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) {
} else if (e2.getX() – e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
} catch (Exception e) {
// nothing
return false;

public interface onAction{
void doAction();

private void onL2RDoAction(){

private void onR2LDoAction(){



The main component here is the way to determine that the action of flinging has happened.
By defining the constants of minimum distance and minimum velocity that calculated from 2 points: touching point and releasing point

The demo that uses this snippet of code can be found here

One SMALL notice when applying the gesture listener on some kinds of view is  that view must be set to be clickable ( clickable = true)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: