Android tip #010 – Left and right swipe (gesture events)

Platform/Language: Java/Android

Description: Android has some tools to receive touch events, but with this code, we will received left and right swipes on the whole screen.

Code:

YourActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class YourActivity extends Activity {
  private GestureDetector gestureDetector;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    // ...

    gestureDetector = new GestureDetector(
                      new SwipeGestureDetector());
  }

  /* ... */

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
      return true;
    }
    return super.onTouchEvent(event);
  }

  private void onLeftSwipe() {
    // Do something
  }

  private void onRightSwipe() {
    // Do something
  }

  // Private class for gestures
  private class SwipeGestureDetector
          extends SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                         float velocityX, float velocityY) {
      try {
        float diffAbs = Math.abs(e1.getY() - e2.getY());
        float diff = e1.getX() - e2.getX();

        if (diffAbs > SWIPE_MAX_OFF_PATH)
          return false;
       
        // Left swipe
        if (diff > SWIPE_MIN_DISTANCE
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
           YourActivity.this.onLeftSwipe();

        // Right swipe
        } else if (-diff > SWIPE_MIN_DISTANCE
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
          YourActivity.this.onRightSwipe();
        }
      } catch (Exception e) {
        Log.e("YourActivity", "Error on gestures");
      }
      return false;
    }
  }
}

You may also like...

14 Responses

  1. Lavi says:

    Left swipe and right swipe conditions are the same…

    The right swipe you could choose:
    ..} else if (-diff > SWIPE_MIN_DISTANCE && …

  2. ERiDeM says:

    Thanks for the tip ;-)
    I’ve fixed the code.

  3. hans says:

    Thank you for this example. however I had to seacrh quite a lot to get it run… This was due to the onTouchEvent method missing. After I have added the code below it worked smoothly

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
    return true;
    }
    return super.onTouchEvent(event);
    }

  4. Andy says:

    I am trying to use your example, but its not working. I am just testing to see if it listens to anything, but nothing happens when I try to swipe. Any ideas?

  5. ERiDeM says:

    Thanks for the fix.

    I add the missing method to the code.

    Andy, please check it out again :)

  6. efe says:

    thanks for the code. it was so helpful.

  7. me says:

    Nice simple code and works perfectly. Thanks.

  8. Ganesh says:

    When I used the above code at
    gestureDetector = new GestureDetector(new SwipeGestureDetector());

    I get the SwipeGestureDetector() a red line saying that it has deprecated in API Level 3. what alternative should I need to use for this?

    • ERiDeM says:

      I’ve been using this class from API 7 without problems. I do not really know why it shows as deprecated for you :(

    • droidie says:

      That method is depecated. You have to pass in the context as well as the SimpleGestureDetector()

      Like this:
      Assuming you have set up a Context variable called context.

      gestureDetector = new GestureDetector(context, new SwipeGestureDetector());

  9. Terence says:

    Thanks. Exactly what I was looking for. Also works perfectly.

  10. Newbie says:

    Can you provide soure code to download for this tutorial? Cuz I’m newbie.

    • ERiDeM says:

      Hi Newbie,

      Sorry, I think that the example is quite well documented. Try creating a small application that contains an Activity and reuse my example on that activity.