Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to handle right to left swipe gestures

I want my app to recognize when a user swipes from right to left on the phone screen.

How to do this?

like image 233
endryha Avatar asked Nov 09 '10 22:11

endryha


People also ask

How do I change left and right swipe on Android?

Tap General settings. Find Mail swipe actions and tap it. Tap Change next to either Right swipe or Left swipe. From there, you can adjust what that directional swipe does.

How do I change the swipe left on my Android phone?

Changing your swipe actionsTap the settings icon. Tap General. Scroll down to Swipe actions at the bottom of the screen. Choose whether you want to Select, Complete or Schedule a task when swiping left or right.


1 Answers

OnSwipeTouchListener.java:

import android.content.Context; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener;  public class OnSwipeTouchListener implements OnTouchListener {      private final GestureDetector gestureDetector;      public OnSwipeTouchListener (Context ctx){         gestureDetector = new GestureDetector(ctx, new GestureListener());     }      @Override     public boolean onTouch(View v, MotionEvent event) {         return gestureDetector.onTouchEvent(event);     }      private final class GestureListener extends SimpleOnGestureListener {          private static final int SWIPE_THRESHOLD = 100;         private static final int SWIPE_VELOCITY_THRESHOLD = 100;          @Override         public boolean onDown(MotionEvent e) {             return true;         }          @Override         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {             boolean result = false;             try {                 float diffY = e2.getY() - e1.getY();                 float diffX = e2.getX() - e1.getX();                 if (Math.abs(diffX) > Math.abs(diffY)) {                     if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {                         if (diffX > 0) {                             onSwipeRight();                         } else {                             onSwipeLeft();                         }                         result = true;                     }                 }                 else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {                     if (diffY > 0) {                         onSwipeBottom();                     } else {                         onSwipeTop();                     }                     result = true;                 }             } catch (Exception exception) {                 exception.printStackTrace();             }             return result;         }     }      public void onSwipeRight() {     }      public void onSwipeLeft() {     }      public void onSwipeTop() {     }      public void onSwipeBottom() {     } } 

Usage:

imageView.setOnTouchListener(new OnSwipeTouchListener(MyActivity.this) {     public void onSwipeTop() {         Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();     }     public void onSwipeRight() {         Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();     }     public void onSwipeLeft() {         Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();     }     public void onSwipeBottom() {         Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();     }  }); 
like image 194
Mirek Rusin Avatar answered Sep 25 '22 12:09

Mirek Rusin