Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the swipe left or Right in Android?

I have an EditText view in android. On this I want to detect swipe left or right. I am able to get it on an empty space using the code below. But this does not work when I swipe on an EditText. How do I do that? Please let me know If I am doing something wrong. Thank you.

Code Used:

switch (touchevent.getAction()) {     case MotionEvent.ACTION_DOWN:     {         oldTouchValue = touchevent.getX();         break;     }     case MotionEvent.ACTION_UP:     {         float currentX = touchevent.getX();         if (oldTouchValue < currentX)         {             // swiped left         }         if (oldTouchValue > currentX )         {             swiped right         }     break;     } } 
like image 396
Vinod Avatar asked Jul 11 '11 03:07

Vinod


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 settings on my Samsung?

Under Full screen gestures, tap the More Options button below the Swipe gestures option. Tap on Swipe from bottom or Swipe from sides and bottom, depending on your preference. Tap on the slider at the bottom of the screen to customize the Back gesture sensitivity.


1 Answers

Simplest left to right swipe detector:

In your activity class add following attributes:

private float x1,x2; static final int MIN_DISTANCE = 150; 

and override onTouchEvent() method:

@Override public boolean onTouchEvent(MotionEvent event) {          switch(event.getAction())     {       case MotionEvent.ACTION_DOWN:           x1 = event.getX();                                 break;                 case MotionEvent.ACTION_UP:           x2 = event.getX();           float deltaX = x2 - x1;           if (Math.abs(deltaX) > MIN_DISTANCE)           {               Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show ();           }           else           {               // consider as something else - a screen tap for example           }                              break;         }                 return super.onTouchEvent(event);         } 
like image 69
user2999943 Avatar answered Oct 01 '22 04:10

user2999943