You need first to create a class extended from gesturedetector.simpleongesturelistener . And inside create three variables .
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
SWIPE_MIN_DISTANCE is the minimal distance you need to make that will be receive by the Android device.
SWIPE_THRESHOLD_VELOCITY is the speed you need to do.
SWIPE_MAX_OFF_PATH is the error limit you can do . For example you swipe diagonally, the Android device will know if you’re trying to swipe up or right .
Now create the method onFling and put this calculs . It’s really complicated so i’m just giving you this without any explications .
This should look like this .
class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float dX = e2.getX()-e1.getX();
float dY = e1.getY()-e2.getY();
if (Math.abs(dY)<SWIPE_MAX_OFF_PATH &&
Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {
if (dX>0) {
Toast.makeText(getApplicationContext(), “Right Swipe”, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), “Left Swipe”, Toast.LENGTH_SHORT).show();
}
return true;
} else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH &&
Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {
if (dY>0) {
Toast.makeText(getApplicationContext(), “Up Swipe”, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), “Down Swipe”, Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}
Now on the oncreate you’ll need to implement the ontouch on the view .
gesturedetector = new GestureDetector(new MyGestureListener());
Choose the root of your layout (it’s more conveniant) .
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gesturedetector.onTouchEvent(event);
return true;
}
});
But sometimes you have a ScrollView inside the layout and the gestures can’t be detected so just do .
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return gesturedetector.onTouchEvent(ev);
}
This should look like this :
public class Test extends Activity{
private GestureDetector gesturedetector = null;
View layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
layout = (LinearLayout)findViewById(R.id.container);
gesturedetector = new GestureDetector(new MyGestureListener());
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gesturedetector.onTouchEvent(event);
return true;
}
});
}
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return gesturedetector.onTouchEvent(ev);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float dX = e2.getX()-e1.getX();
float dY = e1.getY()-e2.getY();
if (Math.abs(dY)<SWIPE_MAX_OFF_PATH &&
Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {
if (dX>0) {
Toast.makeText(getApplicationContext(), “Right Swipe”, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), “Left Swipe”, Toast.LENGTH_SHORT).show();
}
return true;
} else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH &&
Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {
if (dY>0) {
Toast.makeText(getApplicationContext(), “Up Swipe”, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), “Down Swipe”, Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}
}
Nice Tutorial
Thanks!!! Works very fine!
I cant understand how to proceed at this step
layout=(LinearLayout) findViewById(R.id.container);
but my main_activity does not have any layout’s how can I Proceed here.
Pingback: Galleryview-Differentiate touch and swipe : Android Community - For Application Development
your code is great and clean
thanks man you saved lots of time n troubles
Thank you, this was very helpful 🙂
One note: this constructor gesturedetector = new GestureDetector(new MyGestureListener()); is now depricated. You can update it by adding Context as first parameter.
Besides that, great.
Don’t you think you should share the source code as well? for a newbie it will be really helpfull.