I have a edittext view in android. On this I want to detect a swipe left or right. I am able to get it on a empty space with the below code. But this does not work when I swipe on a edittext. How do i do it? 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;
}
}
I wrote a simple class that makes it easy to detect the swipe events - TOP, RIGHT, BOTTOM, LEFT.
1: Detect single swipe event
2: Detect any of the swipe events with one callback.
Here is a blog post with the explanation on how to use: http://bmutinda.com/android-detect-swipe-events/
I have also created a Gist for the code snippets available here: https://gist.github.com/bmutinda/9578f70f1df9bd0687b8
Thanks.
Swipe events are a kind of
onTouch
events. Simply simplifying @Gal Rom 's answer, just keep track of the vertical an horizontal deltas, and with a little math you can determine what kind of swipe a touchEvent was. (Again, let me stress that this was OBSENELY based to a previous answer, but the simplicity may appeal to novices). The idea is to extend an OnTouchListener, detect what kind of swipe (touch) just happened and call specific methods for each kind.the best answer is @Gal Rom 's. there is more information about it: touch event return's to child views first. and if you define onClick or onTouch listener for them, parnt view (for example fragment) will not receive any touch listener. So if you want define swipe listener for fragment in this situation, you must implement it in a new class:
I changed @Gal Rom 's class. So it can detect both right and left swipe and specially it returns onInterceptTouchEvent true after detect. its important because if we dont do it some times child views maybe receive event and both of Swipe for fragment and onClick for child view (for example) runs and cause some issues. after making this class, you must change your fragment xml file:
you see that begin tag is the class that we made. now in fragment class:
It's a little complicated but works perfect :)
After a full day working on this feature finally able to get the right answer.
Good luck .
I want to add on to the accepted answer which works partially, but is missing the time variable, that makes it perfect.
Simplest left to right swipe detector with a time variable:
In your activity class add following attributes:
and override onTouchEvent () method:
Detect swipe in four direction
and