Gesture?
Gestures in android is defined as the movements of your fingers on android device screen and respond accordingly if there is any listener is implemented for gesture detection.
In this article we can see how to do action by SwipeTop, SwipeBottom, SwipeRight, SwipeLeft.
Let's Start
Create a class OnSwipeTouchListener
That's it
________________________________________________________________
Gestures in android is defined as the movements of your fingers on android device screen and respond accordingly if there is any listener is implemented for gesture detection.
In this article we can see how to do action by SwipeTop, SwipeBottom, SwipeRight, SwipeLeft.
Let's Start
Create a class OnSwipeTouchListener
public class OnSwipeTouchListener implements View.OnTouchListener { private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); public boolean onTouch(final View v, final MotionEvent event) { return gestureDetector.onTouchEvent(event); } private final class GestureListener extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_THRESHOLD = 100; private static final int SWIPE_VELOCITY_THRESHOLD = 100; @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) { result = onSwipeRight(); } else { result = onSwipeLeft(); } } } else { if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (diffY > 0) { result = onSwipeBottom(); } else { result = onSwipeTop(); } } } } catch (Exception exception) { exception.printStackTrace(); } return result; } } public boolean onSwipeRight() { return false; } public boolean onSwipeLeft() { return false; } public boolean onSwipeTop() { return false; } public boolean onSwipeBottom() { return false; } }
Now open the Activity where you want a swipe gesture
If you need a swiping action anywhere in an Activity then select the Parent Layout as a view to apply gesture
yourView.setOnTouchListener(new OnSwipeTouchListener() {
public boolean onSwipeTop() {
//do your action
return true;
}
public boolean onSwipeRight() {
public boolean onSwipeTop() {
//do your action
return true;
}
public boolean onSwipeRight() {
//do your action
return true;
}
public boolean onSwipeLeft() {
}
public boolean onSwipeLeft() {
//do your action
return true;
}
public boolean onSwipeBottom() {
return true;
}
public boolean onSwipeBottom() {
//do your action
return true;
}
});
return true;
}
});
That's it
________________________________________________________________
Happy Coding...
Comments
Post a Comment