Android swipe gesture implementation

I was looking for a simple solution how to swipe screens left and right.
I found some on this page “Android Gestures

It is so simple, that I do want to show it here. But I needed change a little code for my needs and Eclipse asked two times to remove “@Override” option.

First, create SimpleGestureFilter class in your projest:

public class SimpleGestureFilter extends SimpleOnGestureListener{
 public final static int SWIPE_UP    = 1;
 public final static int SWIPE_DOWN  = 2;
 public final static int SWIPE_LEFT  = 3;
 public final static int SWIPE_RIGHT = 4;
 public final static int MODE_TRANSPARENT = 0;
 public final static int MODE_SOLID       = 1;
 public final static int MODE_DYNAMIC     = 2;
 private final static int ACTION_FAKE = -13; //just an unlikely number
 private int swipe_Min_Distance = 100;
 private int swipe_Max_Distance = 350;
 private int swipe_Min_Velocity = 100;
 private int mode      = MODE_DYNAMIC;
 private boolean running = true;
 private boolean tapIndicator = false;
 private Activity context;
 private GestureDetector detector;
 private SimpleGestureListener listener;
 public SimpleGestureFilter(Activity context,SimpleGestureListener sgl) {
  this.context = context;
  this.detector = new GestureDetector(context, this);
  this.listener = sgl; 
 public void onTouchEvent(MotionEvent event){
   boolean result = this.detector.onTouchEvent(event); 
   if(this.mode == MODE_SOLID)
   else if (this.mode == MODE_DYNAMIC) {
     if(event.getAction() == ACTION_FAKE) 
     else if (result)
     else if(this.tapIndicator){
      this.tapIndicator = false;
   //else just do nothing, it's Transparent
 public void setMode(int m){
  this.mode = m;
 public int getMode(){
  return this.mode;
 public void setEnabled(boolean status){
  this.running = status;
 public void setSwipeMaxDistance(int distance){
  this.swipe_Max_Distance = distance;
 public void setSwipeMinDistance(int distance){
  this.swipe_Min_Distance = distance;
 public void setSwipeMinVelocity(int distance){
  this.swipe_Min_Velocity = distance;
 public int getSwipeMaxDistance(){
  return this.swipe_Max_Distance;
 public int getSwipeMinDistance(){
  return this.swipe_Min_Distance;
 public int getSwipeMinVelocity(){
  return this.swipe_Min_Velocity;
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {

  final float xDistance = Math.abs(e1.getX() - e2.getX());
  final float yDistance = Math.abs(e1.getY() - e2.getY());

  if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance)
   return false;

  velocityX = Math.abs(velocityX);
  velocityY = Math.abs(velocityY);
        boolean result = false;

  if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){
   if(e1.getX() > e2.getX()) // right to left
   result = true;
  else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance){
   if(e1.getY() > e2.getY()) // bottom to up 
   result = true;

   return result;

 public boolean onSingleTapUp(MotionEvent e) {
  this.tapIndicator = true;
  return false;

 public boolean onDoubleTap(MotionEvent arg0) {
  return true;

 public boolean onDoubleTapEvent(MotionEvent arg0) {
  return true;

 public boolean onSingleTapConfirmed(MotionEvent arg0) {
  if(this.mode == MODE_DYNAMIC){        // we owe an ACTION_UP, so we fake an       
     arg0.setAction(ACTION_FAKE);      //action which will be converted to an ACTION_UP later.                                    
  return false;
    static interface SimpleGestureListener{
     void onSwipe(int direction);
     void onDoubleTap();

then implement Gesture Listener in your code. In my case it looked as following:

public class myClass extends ListActivity implements SimpleGestureListener{
	private SimpleGestureFilter detector; 

    public void onCreate(Bundle savedInstanceState) {
        detector = new SimpleGestureFilter(this,this);
    public boolean dispatchTouchEvent(MotionEvent me){ 
     return super.dispatchTouchEvent(me); 
 public void onSwipe(int direction) {
  String str = "";
  switch (direction) {
  case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";
  case SimpleGestureFilter.SWIPE_LEFT :  str = "Swipe Left";
  case SimpleGestureFilter.SWIPE_DOWN :  str = "Swipe Down";
  case SimpleGestureFilter.SWIPE_UP :    str = "Swipe Up";
   Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

 public void onDoubleTap() {
    Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show(); 



  • At 2012.07.05 10:00, Moishe Beshkin said:

    nothing special – just your own layout xml file.

    • At 2014.03.07 09:38, hamid said:

      thank you

    • At 2012.07.09 07:00, Dhana said:

      Thanks a lot. This code more helped.

    • At 2012.07.12 14:34, Royalblue said:

      Thanks a lot. :-)

      • At 2012.08.16 06:49, Brian said:

        Thanks a bunch! This really works well.

        • At 2012.08.20 09:23, Sruti said:

          Unfortunately application has stopped message is coming .Someone please help me

          • At 2012.08.27 15:45, Alex said:

            Thank you sooo much!!!!! this blog post has helped me out so much! my only issue is that the toasts do not appear every time I swipe, however this is the only tutorial out of the tens of them I have already tried that actually worked!!!!! Thank you once again.

            • At 2012.08.28 18:40, docslax said:

              Hi Alex,
              The toasts may not appear based on the length of your swipes.
              Note the following values:

              private int swipe_Min_Distance = 100;
              private int swipe_Max_Distance = 350;
              private int swipe_Min_Velocity = 100;

              These values control the swipe, anything shorter than a 100 and longer than 350 is ignored. Which depending on your screen size could be a very short distance as it states in the Documentation that “For a touch screen, reports the absolute X screen position of the center of the touch contact area. The units are display pixels.”

              Hope that clears it up for you.

              Thanks to OP for this great and simple to implement tutorial, came in really handy when I needed to figure out how it worked ;)

            • At 2012.09.10 19:16, Rob said:

              Will this work for swiping back and forth between different layouts in a fragment, without affecting the other fragment(s) in the activity? I realize that there would be some modifications needed for sure, but would the changes required be extensive? Anyone? And thanks for the post.

              • At 2012.09.19 13:13, Henk said:

                Works directly, is rare these days.
                Many thanks

                • At 2012.09.29 13:12, Tuomas said:


                  • At 2012.10.11 06:41, zubi said:

                    thanks,But can I set swipe action for a single items like image or listview,etc.If anybody knows please tell me where to put that code.

                    • At 2012.10.18 07:43, Rohit said:

                      Sir, i have implemented it successfully but messages in the toasts do not appear whenever i swipe…. please help!!!!!!!!!!!!!!

                      • At 2012.10.18 08:08, Moishe Beshkin said:

                        try to use myClass.this instead of this in Toast.

                        • At 2012.10.18 09:33, Rohit said:

                          Sir, actually i was trying to swipe between tabs. So i have not implemented toast. instead of toast, i used intent. but implementing toast also does not help.

                          • At 2012.10.18 09:37, Moishe Beshkin said:

                            I haven’t try it on different Activities.
                            I think that you should implement this code in every Activity you are openning
                            Or your Activities are not opened?

                            • At 2012.10.18 09:43, Rohit said:

                              Ok Sir, i will try. Thanks for your help. Pleasure to talk with you. Do u have facebook account?

                    • At 2012.11.01 10:07, Sascha said:

                      First thank you so much. Everyhing works real fine!
                      But my problem is now that i want that gesture only when i swipe over a special linear layout and not over the whole screen.
                      Can u help me?

                    • At 2012.11.05 09:02, Vikram said:

                      Hey thank you for this! It works brilliantly. However, if i want to detect motion event on a particular view, how do i do that?? Like i have two fragments, one on the left and the other on the right. I want to apply motion listener to the right fragment only. Any idea??

                      • At 2012.11.05 09:10, Moishe Beshkin said:

                        This question was raised before in the comments.
                        I haven’t tried to do it myself. So, if you will find any solution, tell us.

                        I know, that on every event can be set definite touch listener:

                        GestureDetector mGestureDetector = new GestureDetector(this, mGestureListener);
                        element.setOnTouchListener(new View.OnTouchListener() {
                        	        public boolean onTouch(View v, MotionEvent event) {
                        	            return mGestureDetector.onTouchEvent(event);

                        mGestureDetector is a connector to listener:

                        	 * Gesture Event Handler
                        	private SimpleOnGestureListener mGestureListener = new SimpleOnGestureListener() {
                        		public boolean onDown(MotionEvent e) {
                        			Log.i(TAG, "[CALLBACK_GL] boolean onDown(e:" + e + ")");
                        			return super.onDown(e);
                                public boolean onSingleTapUp(MotionEvent e)
                                     * Do your stuff
                                    return super.onSingleTapUp(e);
                        		public boolean onSingleTapConfirmed(MotionEvent e) {
                        			Log.i(TAG, "[CALLBACK_GL] boolean onSingleTapConfirmed(e:" + e + ")");
                        			return super.onSingleTapConfirmed(e);
                        		public boolean onDoubleTap(MotionEvent e) {
                        			Log.i(TAG, "[CALLBACK_GL] boolean onDoubleTap(e:" + e + ")");
                        			return super.onDoubleTap(e);
                        		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        			Log.i(TAG, "[CALLBACK_GL] boolean onFling(e1:" + e1 + ", e2:" + e2 + ", velocityX:" + velocityX
                        					+ ", velocityY:" + velocityY + "");
                        			return super.onFling(e1, e2, velocityX, velocityY);
                        		public void onShowPress(MotionEvent e) {
                        			Log.i(TAG, "[CALLBACK_GL] void onShowPress(e:" + e + ")");
                        		public void onLongPress(MotionEvent e) {
                        			Log.i(TAG, "[CALLBACK_GL] void onLongPress(e:" + e + ")");
                        • At 2012.11.05 09:59, Vikram said:

                          Thanks for your reply. I tried this but it doesn’t go into onFling. Only onDown, onShowPress and onLongPress is called. Something else needs to be done??

                      • At 2012.11.05 10:24, Vikram said:

                        I made it work! You have to return true inside onDown() and it works! Thanks again! Here’s where i found it:

                        • At 2012.11.09 11:32, Karas said:

                          I use your solution in combination with an ActionBar. But now i can’t select an OptionItem. What do I have to do for using swipe gestures and detecting a singleTap on an Option Item???

                          • At 2012.11.23 13:47, kamlesh said:

                            good tutorial.
                            but how to detect on which list item swipe event is performed.

                            • At 2012.11.23 14:06, kamlesh said:

                              I am getting error
                              Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘’

                              I have a list View in .xml with id list.
                              still getting error. please give me a solution. thanks

                              • At 2012.11.24 20:33, Moishe Beshkin said:

                                check your project. if needed, make clean project from Eclipse menu

                              • At 2012.11.27 07:03, Rohit said:

                                Sir, i have implemented your code. It works awesomely good but i want to apply it on my ImageView only. can you tell me how to do it? Please help!!!!!!!

                                • At 2012.11.27 07:43, Moishe Beshkin said:

                                  Rohit, check my answer to Vikram.
                                  It is possible to apply swipe gesture listener to any element.

                                • At 2012.11.27 07:55, Rohit said:

                                  I have to add only following method
                                  imageView.setOnTouchListener(new View.OnTouchListener()
                                  public boolean onTouch(View v, MotionEvent event) {
                                  return mGestureDetector.onTouchEvent(event)

                                  Remaining all will remain same na??

                                  • At 2012.11.27 07:59, Moishe Beshkin said:

                                    yes, should work.

                                    • At 2012.11.27 08:41, Rohit said:

                                      its not working Sir. can u please help me with example?

                                  • At 2012.12.30 12:31, mvsagar said:

                                    Thank you for your code. It worked and reduced a lot of effort from me!

                                    • At 2013.01.12 16:10, Balachandar KM said:

                                      Thank you so much. So cool and worked without any prob.

                                      • [...] detail activity of the item to show the detail view of next item in the list. I am following this but when i swipe from left to right next view is not displaying just getting a toast message [...]

                                        • At 2013.02.15 21:40, jiioh said:

                                          hi sir.. we are creating a java application that most likely story teller for our thesis.. but we are having a problem about swiping left and right from the 1st activity to another activity (from page 1 to page 2) how can i implement this codes.. please help us..

                                          • At 2013.02.16 16:55, Moishe Beshkin said:

                                            I would help you with honor. But if it is commercial project, I would ask for some reward on my knowledge and help.
                                            contact me for details.


                                          • At 2013.02.18 11:07, Prashant said:

                                            Hi, i get the next error:
                                            06-25 17:36:26.229: E/AndroidRuntime(21264): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘’
                                            could u help me, please?

                                            • At 2013.02.18 18:44, Moishe Beshkin said:

                                              you should add following line to ListView in your layout

                                            • At 2013.02.18 13:32, joe said:

                                              Would like to check with you, what parameter can be adjusted to set the sensitivity?
                                              Many Thanks!

                                              • At 2013.02.18 18:45, Moishe Beshkin said:

                                                Check the following values:
                                                private int swipe_Min_Distance = 100;
                                                private int swipe_Max_Distance = 350;
                                                private int swipe_Min_Velocity = 100;

                                                you should play with them in order to adjust sensitivity

                                                • At 2013.02.19 15:31, jiioh said:

                                                  sorry but i just want to know what and where i need to put the code that when i swipe to left it will go to the next activty.. please..

                                                  • At 2013.02.20 14:33, Moishe Beshkin said:

                                                    you should add the following code:

                                                    case SimpleGestureFilter.SWIPE_LEFT :  str = "Swipe Left";
                                                    startActivity(new Intent(this, NextActivity.class));
                                                  • At 2013.02.19 15:31, jiioh said:

                                                    sorry but i just want to know what and where i need to put the code that when i swipe to left it will go to the next activity.. please..

                                                    • At 2013.03.10 17:32, Dan webb said:

                                                      Hi great Moishe,
                                                      I’ve implemented the swipe functionality ok but how do I prevent the user from swiping repeatedly? I’d like the swipe functionality to wait 1 second before it allows the gesture to be repeated.

                                                      I think it should be in the e2 < gesture code but have not seen an example yet.

                                                      • At 2013.04.08 05:57, wawe said:

                                                        hi,, i have a few questions :
                                                        1. what is mode used for??
                                                        2. what is the differences between 3 mode that exist in your code and what is it used for??

                                                        • At 2013.04.26 14:23, Name said:

                                                          the interface SimpleGestureListener should be public

                                                          • At 2013.04.26 19:18, Harris Jeangilles said:

                                                            hi,, i have a few questions :
                                                            1. what is mode used for??
                                                            2. what is the differences between 3 mode that exist in your code and what is it used for??

                                                            • 18 old comments are not displayed. Click to display all comments
                                                            (Required, will not be published)