1. What Is A Swipe Gesture In Android?
Swipe gesture is just as other gestures provided by the android. Android supports various kinds of gestures. Single tap, double-tap, scrolling, pinch, long presses, and fling, are the most used gestures.
2. How Android Detect Swipe Gesture ?
When a user makes a swipe on the screen, the android OS will calculate the distance moved by the finger. In other words it find the co-ordinates of start point and end point of a gesture and calculate the difference by using that.
And it is also very important to note that the time taken to complete this action is also the factor which decide type gesture. In other words it is also called as velocity specifically when it comes to the swipe gesture detection
3. Why You should implement swipe gesture in your app ?
User experience is the most important factor for the app success. And that is where swipe gestures helps developer to create a great user experience and thus make your app stand out.
Again not every app has the necessity to implement the swipe gesture detection.
4. When Should I Implement the swipe gesture in my app?
You might have come across various apps that implemented swipe gestures to move to various activities or some specific actions
5. Detecting The Swipe Gesture In Android
Now let us understand how to implement the swipe gesture detection in android step by step.
Step 1 : Setting Up The Project
Open the android studio and create a new project and give any name you prefer.
Step 2 : Understanding Interfaces And Classes Required
Step 3 : Creating SwipeActions Interface
Our SwipeActions interface will 4 methods or functions to the class which implements it. They will help to handle the things we want to do after the swipe. So create an interface called SwipeActions and copy the code below
Step 4 : Creating SwipeGestureDetector Class
Step 5 : activity_main.xml
Our activity_main.xml will contain a text view and LinearLayout as a root layout.
TextView will display which side the swipe as happened. And root LinearLayout will be used to detect the swipe events. The Id’s given to them will be used in MainActivity.java
activity_main.xml will look something like this
Step 6 : MainActivity.java
Our MainActivity.java will basically do the fallowing things
- It takes the view reference of root layout i.e LinearLayout and TextView and store it in there respective kind of variable.
- It will create a new instance of SwipeGestureDetector class that we created earlier.
- A GestureDetectorCompat class instance is created by passing the context and the SwipeGestureDetector instance that created earlier
- setOnTouchListener() will be called on the rootLayout to detect the touch events
- Inside that onTouchListener gestureDetectorCompat object will handle the events by calling onTouchEvent()
6. Final Words
That’s All about detecting swipe gestures in android. You can take example project code from below. Feel free to ask any doubts if you get any or don’t understand any part of the code. I will be happy to assist you
Check out source code from Github