Android Swipe Gesture - Androhub

Swipe Gesture Listener

Android Swipe Gesture

The Android SDK supports a variety of gestures you can detect, allowing you to tailor the ways in which users interact with your applications. In this tutorial, we will look at the framework that allows developers to add gesture detection to a simple application, working through the process of supporting the fling gesture and outlining how to support other gestures. Once you’ve completed the development for a basic fling function, you’ll be able to add additional gestures to the application yourself.

Example

In this tutorial, we are going to learn how to implement various swipe gestures in an app.

Video Demo

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is ShareActivity.java.

2. First create an xml layout file naming activity_main.xml that contains several imageview indicating type of gestures that a user can do.

3. Now create a java class naming SimpleGestureFilter.java that conatins various Gesture detector methods that we can use in our activity to recognize user gesture. You can modify this class according to your need.

4. Finally come to your MainActivity.java and add the following code. In this class we do several things listed below:

  • Implement SimpleGestureListener interface to listen user gesture.
  • Make an instance of SimpleGestureFilter class that we had created above.
  • Finally use onSwipe(int direction) and onDoubleTap method to do your stuff.

5. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

 

4 Comments

Eswar
Thursday, January 3rd, 2019

How can we use this for Fragment?

Dr. Droid
Thursday, January 3rd, 2019

Hi Eswar,

In fragment wherever you want to use Activity as Context you can use getActivity().

Thanks

Azriidros
Tuesday, February 5th, 2019

Hello. How can we save this gestures in database?

Dr. Droid
Wednesday, February 6th, 2019

Hi Azriidros

In post we are displaying a toast whenever a gesture is detected. So there you have to save the gesture details in database.

If you don’t have any knowledge about Sqlite database then check this link.

Thanks

Post comment

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