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 like the code from @user2999943. But just some minor changes for my own purposes.
Simplest left to right swipe detector:
In your activity class add following attributes:
and override
onTouchEvent()
method:I think what you want is called a fling. The MotionEvents can be used to determine the direction of the fling.
http://developer.android.com/training/gestures/detector.html
This is a cute class I use (in cases I want to catch event on a View, if it is a ViewGroup, I use the second implementation):
and this is a use example:
In some cases you would like to detect the swipe gestures on a container and pass down the touch Events to the childs so in that case you can create a Custom View group, lets say RelativeLayout and override onInterceptTouchEvent , and there you can detect the swipe event without blocking the pass of Touch Event to your child views,for Example:
Left to Right and Right to Left Swipe Detector
Firstly, declare two variable of float datatype.
Secondly, wireup your xml view in java. Like I have
ImageView
Thirdly,
setOnTouchListener
on yourImageView
.