Search code examples
androidswipeswipe-gestureonfling

Android left to right swipe only


I am a beginner in Android. I need to swipe only from left to right and to do not swipe from right to left. For this what I want to do? All the examples are showing both left to right and right to left?


Solution

  • Take a look at the accepted solution here - Fling gesture detection on grid layout

    The solution explains how to implement a simple gesture listener, and record left to right and right to left swipes. The relevant code from the example is in the GestureDetector onFling override method. Basically, if point 1 (e1) minus point 2 (e2) is greater than a set minimum swipe distance, then you know it's right to left. If point 2 (e2) minus point 1 (e1) was greater than the minimum swipe distance then it's left to right.

    // right to left swipe
    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
    }  
    // left to right swipe
    else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
         Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
    }