Skip to content

Instantly share code, notes, and snippets.

@bmutinda
Last active May 2, 2023 22:38
Show Gist options
  • Save bmutinda/9578f70f1df9bd0687b8 to your computer and use it in GitHub Desktop.
Save bmutinda/9578f70f1df9bd0687b8 to your computer and use it in GitHub Desktop.
Android swipe events detector
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/swipeElement"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#FFD"
android:clickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/abc_text_size_title_material"
android:text="@string/swipe_element"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
package bmutinda.com.androidutils.libs;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Mutinda Boniface on 7/5/2015.
*/
public class SwipeEvents implements View.OnTouchListener {
private SwipeCallback swipeCallback;
private SwipeSingleCallback swipeSingleCallback;
private SwipeDirection detectSwipeDirection;
float x1, x2, y1, y2;
View view;
private static SwipeEvents newInstance(){
return new SwipeEvents();
}
public static void detect( View view, SwipeCallback swipeCallback ){
SwipeEvents evt = SwipeEvents.newInstance();
evt.swipeCallback = swipeCallback;
evt.view = view;
evt.detect();
}
public static void detectTop( View view, SwipeSingleCallback swipeSingleCallback){
SwipeEvents evt = SwipeEvents.newInstance();
evt.swipeSingleCallback = swipeSingleCallback;
evt.view = view;
evt.detectSingle(SwipeDirection.TOP);
}
public static void detectRight( View view, SwipeSingleCallback swipeSingleCallback){
SwipeEvents evt = SwipeEvents.newInstance();
evt.swipeSingleCallback = swipeSingleCallback;
evt.view = view;
evt.detectSingle(SwipeDirection.RIGHT);
}
public static void detectBottom( View view, SwipeSingleCallback swipeSingleCallback){
SwipeEvents evt = SwipeEvents.newInstance();
evt.swipeSingleCallback = swipeSingleCallback;
evt.view = view;
evt.detectSingle(SwipeDirection.BOTTOM);
}
public static void detectLeft( View view, SwipeSingleCallback swipeSingleCallback){
SwipeEvents evt = SwipeEvents.newInstance();
evt.swipeSingleCallback = swipeSingleCallback;
evt.view = view;
evt.detectSingle(SwipeDirection.LEFT);
}
private void detect( ){
view.setOnTouchListener(this);
}
private void detectSingle( SwipeDirection direction ){
this.detectSwipeDirection = direction;
view.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() ){
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
SwipeDirection direction =null;
float xDiff = x2-x1;
float yDiff = y2- y1;
if( Math.abs(xDiff) > Math.abs(yDiff) ){
if (x1<x2){
direction = SwipeDirection.RIGHT;
}else{
direction = SwipeDirection.LEFT;
}
}else {
if (y1 > y2) {
direction = SwipeDirection.TOP;
} else {
direction = SwipeDirection.BOTTOM;
}
}
// Only trigger the requested event only if there
if ( detectSwipeDirection !=null && swipeSingleCallback !=null ){
if ( direction == detectSwipeDirection ){
swipeSingleCallback.onSwipe();
}
}else{
if ( direction == SwipeDirection.RIGHT ){
swipeCallback.onSwipeRight();
}
if ( direction == SwipeDirection.LEFT ){
swipeCallback.onSwipeLeft();
}
if ( direction == SwipeDirection.TOP ){
swipeCallback.onSwipeTop();
}
if ( direction == SwipeDirection.BOTTOM ){
swipeCallback.onSwipeBottom();
}
}
break;
}
return false;
}
public enum SwipeDirection{
TOP, RIGHT, BOTTOM, LEFT
}
public interface SwipeCallback{
public void onSwipeTop();
public void onSwipeRight();
public void onSwipeBottom();
public void onSwipeLeft();
}
public interface SwipeSingleCallback{
public void onSwipe();
}
}
package bmutinda.com.androidutils;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.Toast;
import bmutinda.com.androidutils.libs.SwipeEvents;
public class SwipeActivity extends ActionBarActivity {
RelativeLayout swipeElement;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
swipeElement = (RelativeLayout) findViewById( R.id.swipeElement );
// Detect and consume all events
SwipeEvents.detect( swipeElement, new SwipeEvents.SwipeCallback() {
@Override
public void onSwipeTop() {
showToast( "Swiped top");
}
@Override
public void onSwipeRight() {
showToast( "Swiped right");
}
@Override
public void onSwipeBottom() {
showToast( "Swiped bottom");
}
@Override
public void onSwipeLeft() {
showToast( "Swiped left");
}
});
// Detect and consume specific events
// {Available methods} - detectTop, detectRight, detectBottom, detectLeft
SwipeEvents.detectTop(swipeElement, new SwipeEvents.SwipeSingleCallback() {
@Override
public void onSwipe() {
showToast("Swiped - detectTop");
}
});
}
public void showToast( String message ){
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
@bmutinda
Copy link
Author

bmutinda commented Jul 5, 2015

Blog post for this snippet is available here http://bmutinda.com/android-detect-swipe-events/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment