How to Detect Swipe Events

In this post, I’m going to show you how to detect swipe events easily using a simple library that only contains two classes. The library, called “swipe”, is quite accurate in detecting swipe events, and saves developers from the unnecessary burden of writing code for this task. As usual, let me explain how you’re going to use it in your project step by step.

First, add the dependency for the library to your module-level “build.gradle” file:

Then, your activity on which you want to detect swipe events should look like the following after you do the initialization and interface implementation:

What I did here is simply initializing the class named “Swipe” inside onCreate() method of the activity, and let the activity implement “SwipeListener” interface provided by the library. You also need to pass touch events to the library by calling its corresponding method inside dispatchTouchEvent() method of the activity.

The library provides us both “on-swiping” and “on-swiped” methods for every direction. So, you can write your code inside whichever one you want according to your needs.

You can check out the library’s GitHub page here: https://github.com/pwittchen/swipe. It also provides an implementation for the ones who prefer reactive way.

Leave a Reply

Your email address will not be published. Required fields are marked *