Step 1 : Create a java class

[java]
import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements 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 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() {
}
}
[/java]

Step 2 : Implement in activity

[java]

yourView.setOnTouchListener(new OnSwipeTouchListener(MyActivity.this) {
public void onSwipeTop() {
Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}

});

[/java]

In Kotlin 

Step 1 :

[java]

import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View

/**
* Created by ARIF HASNAT on 1/19/19.
*/

open class OnSwipeTouchListener : View.OnTouchListener {

private val gestureDetector = GestureDetector(GestureListener())

fun onTouch(event: MotionEvent): Boolean {
return gestureDetector.onTouchEvent(event)
}

private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {

private val SWIPE_THRESHOLD = 100
private val SWIPE_VELOCITY_THRESHOLD = 100

override fun onDown(e: MotionEvent): Boolean {
return true
}

override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
onTouch(e)
return true
}

override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
val result = false
try {
val diffY = e2.y – e1.y
val diffX = e2.x – e1.x
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
//onSwipeRight()
onSwipeLeft()
} else {
//onSwipeLeft()
onSwipeRight()
}
}
} else {
// onTouch(e);
}
} catch (exception: Exception) {
exception.printStackTrace()
}

return result
}
}

override fun onTouch(v: View, event: MotionEvent): Boolean {
return gestureDetector.onTouchEvent(event)
}

open fun onSwipeRight() {}

open fun onSwipeLeft() {}

open fun onSwipeTop() {}

open fun onSwipeBottom() {}
}
[/java]

Step 2 : 

[java]
/***
* Swipe left and right activity
*/

yourView.setOnTouchListener(object : OnSwipeTouchListener() {

override fun onSwipeLeft() {
Log.e("ViewSwipe", "Left")
//toast("Left!")

val intent = Intent(this@MainPage, LeftActivity::class.java);
startActivity(intent)
}

override fun onSwipeRight() {
Log.e("ViewSwipe", "Right")
// toast("Right!")
val intent = Intent(this@MainPage, RightActivity::class.java);
startActivity(intent)
}
})
[/java]

Leave a Reply

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