How to Implement Android Swipe Gestures in Android App Using GestureDetector Class

0
Shares
How to Implement Android Swipe Gestures in Android App Using GestureDetector Class
5 (100%) 1 vote

android swipe gestures

If you own a smartphone, you already know what a gesture is. Gestures are those subtle motions to trigger interactions between the touch screen and the user.

Generally, it lasts for the time between the first touch on the screen to the point when the finger leaves the surface.

In Android, you can easily implement the Android swipe gestures. Android provides a GestureDetector class to receive motion events and tell us that these events correspond to gestures or not.

Today, in this Android Gesture tutorial, we’ll see how to implement Android swipe gestures in Android app using this GestureDetector class.

Let’s Get Started

Add dependecy in build.gradle file

compile 'com.android.support:appcompat-v7:25.3.1'

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.spaceo.gesturedetectdemo.MainActivity">
<TextView
android:id="@+id/tvSwipDescription"
android:gravity="center"
android:text="@string/tvswipDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</RelativeLayout>

MainActivity.Class

package com.spaceo.gesturedetectdemo;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView tvSwipDescription;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeView();
tvSwipDescription.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
public void onSwipeTop() {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.toastTop), Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.toastRight), Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.toastLeft), Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.toastBottom), Toast.LENGTH_SHORT).show();
}
});
}
private void initializeView() {
tvSwipDescription=(TextView) findViewById(R.id.tvSwipDescription);
}
}

OnSwipeTouchListner.class

package com.spaceo.gesturedetectdemo;
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spaceo.gesturedetectdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

And done!

Now of course, in this Android gesture tutorial, we’ve only covered the basics of implementing Android swipe gestures in Android app. But, depending on your app and functionalities of your app, you can go much deeper while making sure to be thoughtful when implementing this powerful feature of Android.

Now, if you’re not completely aware how to use them, you can consult with Android app development company to implement the Android swipe gestures correctly.

Get a free copy of Android Swipe Gesture Demo from Github.

 
0
Shares
 

Have an Idea for Android App? Let's Discuss!