How To Detect Swipe Gesture In Android ?

How To Detect Swipe Gesture In Android?
Share your knowledge.
Do you want to learn “How to detect swipe gesture in android ?”
 
Then you are at the right place, this tutorial blog will help you.
 
By the end of this read, you will understand how to detect swipe gestures in android. It is not a complete beginner guide. Knowing the basic concepts of OOPs (Object Oriented Programming) is the prerequisite for this. I tried to cover most of the important things about this. You can also head over to specific section that you want to read by the help of table of contents.

#DemoApp

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

You might have come across various apps that implemented swipe gestures to move to various activities or some specific actions
 
Example: Tick Tock App uses gesture navigation to perform various actions.
 
There is no specific answer to this. Because it is opinion-based. But according to me if your app offers various kinds of dynamic content, i.e The content changes over a period of time when the user is using the app and your app offers 3 to 4 actions related to that content then it’s better to implement it. You can take Tick Tock app as an example because it is one of the apps that makes the best use of swipe detection.

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

I will be creating 1 interface and 1 classes to implement the swipe gesture detection. They are

  1. SwipeActions (Interface)
  2. SwipeGestureDetector (Class)

 And I am also using the available interface and Classes in android. They are

  1. GestureDetector.OnGestureListener
  2. GestureDetectorCompat

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

SwipeActions.java

Step 4 : Creating SwipeGestureDetector Class

SwipeGestureDetector class will be used to detect the swipe events. It will implements the GestureDetector.OnGestureListener

Just create a SwipeGestureDetector class and copy the code below 

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()

MainActivity.java file

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

I am the guy responsible to make sure that everything that goes through this website has some value and gives some knowledge to the visitors. I have the habit of collecting quotes and I can't imagine my life without programming. I believe in self-education.
Posts created 4

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.