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){
  
   if(!this.running)
  return;  
  
   boolean result = this.detector.onTouchEvent(event); 
  
   if(this.mode == MODE_SOLID)
    event.setAction(MotionEvent.ACTION_CANCEL);
   else if (this.mode == MODE_DYNAMIC) {
  
     if(event.getAction() == ACTION_FAKE) 
       event.setAction(MotionEvent.ACTION_UP);
     else if (result)
       event.setAction(MotionEvent.ACTION_CANCEL); 
     else if(this.tapIndicator){
      event.setAction(MotionEvent.ACTION_DOWN);
      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;
 }
 
 
 @Override
 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
    this.listener.onSwipe(SWIPE_LEFT);
   else
    this.listener.onSwipe(SWIPE_RIGHT);
   
   result = true;
  }
  else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance){
   if(e1.getY() > e2.getY()) // bottom to up 
    this.listener.onSwipe(SWIPE_UP);
   else
    this.listener.onSwipe(SWIPE_DOWN);
   
   result = true;
  }

   return result;
 }

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

 @Override
 public boolean onDoubleTap(MotionEvent arg0) {
  this.listener.onDoubleTap();;
  return true;
 }

 @Override
 public boolean onDoubleTapEvent(MotionEvent arg0) {
  return true;
 }

 @Override
 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.                                    
     this.context.dispatchTouchEvent(arg0);  
  }   
     
  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; 

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

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

}

74 comments

  1. 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..

  2. Hello,
    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.

    Misha.

  3. 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 ‘android.R.id.list’
    could u help me, please?

  4. Hi,
    you should add following line to ListView in your layout
    android:id=”@id/android:list”

  5. 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

  6. 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..

  7. 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..

  8. you should add the following code:

    case SimpleGestureFilter.SWIPE_LEFT :  str = "Swipe Left";
    startActivity(new Intent(this, NextActivity.class));
                                                  break;
    
  9. 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.
    thanks

  10. 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??
    thx

  11. detector = new SimpleGestureFilter(this,this);

    what i have to write when i have a fragment or tablayout plz help me i require …

    Thanks in advance …

  12. i am facing a run time error in line this.detector.onTouchEvent(me); when i use your code in more then two activities.. pls give me your email id i will give you my project please run it and please see why error is coming
    please help me
    thanx in advance

  13. It confused with action zoom an image. When i use 2 fingers to zoom, it called SWIPE_UP and SWIPE_DOWN

Leave a Reply

%d bloggers like this: