Android: How to handle right to left swipe gestures
Asked Answered
T

22

500

I want my app to recognize when a user swipes from right to left on the phone screen.

How to do this?

Tolliver answered 9/11, 2010 at 22:22 Comment(4)
Check this link #937813Laddie
See my answer on how to up/down/left/right swipe gestures #13095994Uganda
Check my library that might be helpful github.com/UdiOshi85/libSwipesPossing
Check accepted answer in Kotlin here : https://mcmap.net/q/74031/-android-how-to-handle-right-to-left-swipe-gesturesLimey
B
919

OnSwipeTouchListener.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() {
    }
}

Usage:

imageView.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();
    }

});
Broker answered 17/10, 2012 at 16:19 Comment(33)
Needs some minor updates. Variables are mismatched in the onTouch method, and exception class is not imported. Would make more sense to just return false from onFling(), instead of initiating a variable, assigning it a value, doing nothing with it, and just returning it. Some parts of onTouch method are incorrect. See #14107793 One helpful tip that would've saved me some time and I hope may help others: when you use this method, you only want to add the "implements" tag to your OnSwipeTouchListener classVenomous
works great, but super.onTouch(view, motionEvent); gives me error warning in eclipse "is undefinied for the type object". Deleting this works great.Nighthawk
Thanks works like a charm but you should add a constructor to OnSwipeTouchListener that receives a context because that constructor of GestureDetector is deprecated since API level 3, and instantiate the GestureDetectorin that constructor.Culmination
this is in the main activity right? and then the list itself how do I load that? my main extends listactivity?Norwich
Shouldn't the THRESHOLD be expressed in dp?Siloum
@Mirek Rusin Hi great code! can you telle me how can I use this code inside a viewPager? in order to recognize if i get a swipe from left to rightDrawback
public GestureDetector getGestureDetector() {return gestureDetector;} method should be add to the OnSwipeTouchListener class and be used inside onTouch() method.Followup
This thing worked for me. Now Can u please tell me how to give different actions(or simply toast) for right side vertical swipe and left side vertical swipe ?Floriaflorian
thanks it worked for me with these modifications : https://mcmap.net/q/74031/-android-how-to-handle-right-to-left-swipe-gesturesVariolous
but "onDown" is never called. as a consequence, my e1 is always null and I can do nothing about it.Whitsuntide
@Mirek Rusin What will happen if I change private static final int SWIPE_THRESHOLD to private final int SWIPE_THRESHOLD ?! I need to set swipe sensitivity in run time !!!Apices
When I add code to touch event and tried to swipe, touch event occurs, too. How can I avoid that?Anis
My fix for this answer is to move onTouch into the OnSwipeTouchListener definition, otherwise my IDE will pop up "accessing private member" errorBoanerges
Note for the View you wanna attach it has to be focusable and clickableNg
This is great, but when I attached it to a listView.setOnTouchListener(), it slowed down the scrolling (even though I was using a ViewHolderand some other optimizations.) To solve this, just don't call onSwipeBottom() and onSwipeTop() and make result = false on those cases. By "slowing down" I mean scrolling (smooth) and then stopping all at once - instead of stopping at a decreasing speedHellgrammite
Isn't this a copy of Thomas Fankhauser's answer? Credit where credit is due, please.Azurite
I'm new to Android. I tried this code. Did not work. Application crashed with Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference Where to write Usage Code?Sushi
please tell me how to swipe smoothly even user swipe somewhat in downward manner , right now I am also using android.support.v4.widget.SwipeRefreshLayout ,when i swipe in some zigzag manner both refresh and swipe try to take placeImpatient
How can I use this in OnItemClickListener for listview?Zobe
Its working nice but how to give color change effect while swipe?Diuresis
How to restrict the user in down swipe i.e swipe bottom in your case , I want to disable down swipe in my case . please let me knowSnowshoe
@AlaaM., how did you make scrolling in a ListView work?Ornas
I switched from ListView to RecyclerView and it helped!Ornas
how to get there is not more item both side (left and right) ... please guide me on this. I need to enable and disable the arrow indicator. ThanksHostetler
does anyone know how to call this 'Usage' in MainActivity on Xamarin.Android?Wendalyn
It might make more sense to make the OnSwipeTouchListener class abstract as well as the onSwipe* functions.Florist
Can I do something like , if I swipe half the screen then on swipeDown() function is called or I get value to when swipe if done half the screen.Nord
Can you add rtl support please? That is onSwipeRight() and onSwipeLeft() get switched when rtlAthwartships
I am getting NullPointerException on Orientation Changes while used in FragmentAmalberga
Its working well, but have a doubt that the onClick is not working for the view the swipe is applied.! @MericBalladist
Kotlin equivalent is here : https://mcmap.net/q/74031/-android-how-to-handle-right-to-left-swipe-gesturesLimey
Greate answer. Works fine, but SwipeRight and onSwipeLeft directions should change. Swap it.Tabathatabb
Can we decrease the sensitivity? If yes then how?Bernard
M
225

This code detects left and right swipes, avoids deprecated API calls, and has other miscellaneous improvements over earlier answers.

/**
 * Detects left and right swipes across a view.
 */
public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public void onSwipeLeft() {
    }

    public void onSwipeRight() {
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_DISTANCE_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) {
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight();
                else
                    onSwipeLeft();
                return true;
            }
            return false;
        }
    }
}

Use it like this:

view.setOnTouchListener(new OnSwipeTouchListener(context) {
    @Override
    public void onSwipeLeft() {
        // Whatever
    }
});
Mutz answered 21/10, 2013 at 22:33 Comment(23)
How do you get a touch event for a listview item?Stuppy
onDown() is never called, so I can't intercept null value of e1 and it remains null, so swipe never worksWhitsuntide
sorry if this sounds thick, but (A) Where does the second piece of code go? (view.setOn...), (B) what is the "context" that is being passed? I'm quite new, appreciate any help! Thanks in advanceStellarator
@Whitsuntide I'm not sure what you're seeing. onDown should be called with e (not e1) if the user moves vertically, but since this code snippet is for left-right swiping, it tells Android to ignore the vertical movement.Mutz
@Stellarator You'll definitely want to get a good book or other resource on Android fundamentals; otherwise, trying to cobble solutions together from StackOverflow will prove frustrating. A good option is the official Training for Android developers. The section Starting an Activity tells about where you would put setOnTouchListener (typically in onCreate). context is the this pointer (unless you're creating a fragment).Mutz
thank you Edward - I'm doing both simultaneously! (cobbling and reading)Stellarator
It works great for swipes, thanx. My problem is that I still want to receive the onTouchEvent when there is no swipe. Can't figure that one out.Invertase
@Lara, I haven't tried this, but you could try overriding SimpleOnGestureListener.onSingleTapConfirmed.Mutz
Thanks, Edward. I also found that changing the return of onDown from true to false does the trick.Invertase
i get error on the context here here: view.setOnTouchListener(new OnSwipeTouchListener(context) how can I solve it?Shotten
@Signo, the activity containing the view provides context. In the common case of adding the view to a fragment, use Fragment.getActivity().Mutz
has anybody extended the code to detect top and down swipes?Spelt
@Spelt Depending on your purpose, you may find useful SwipeRefreshLayout, the SwipeRefreshLayoutBasic sample, or one of its variants.Mutz
How do you handle the android lint warning about ClickableViewAccessibility? (If you know a way, please post at my SO question at #31344951)Azure
listview item is not sliding smoothly I mean if user swipe slightly downward then swipe action is not detected. Any help is appreciated. Thanks in advanceImpatient
Plus one from my side, its very useful and the Tutorial link given by you is also very Interesting. Thanks for double help Edward.Stilt
Thanks, this works, but I have some issues with this code. When gesture starts over a button it is not detected. Any idea why?Siphonophore
@Siphonophore Likely it's because the button is handling the events, leaving nothing for the touch listener underneath. However, you should consider whether you really want a button on a control that supports swiping. The typical UI for such situations is for the swipe action to reveal buttons "behind" the list item. The class above doesn't support those buttons, but there are libraries available that do.Mutz
@EdwardBreyYes, that's whats is happening. When the gesture starts over a button, the button is clicked and the gesture is not detected. On iOS it's the opposite, the gesture has the "priority". What other libraries can be used? No code needed, just some directions. Thanks!Siphonophore
Should I disregard Eclipse's warning OnSwipeTouchListener#onTouch should call View#performClick when a click is detected?Warrantee
@DmitriyR I think so. I don't get that warning in Android Studio.Mutz
Getting this error JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' on a null object reference why this error is thrown?Handicapper
NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' on a null object referenceGurl
I
57

If you also need to process click events here some modifications:

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View v, final 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 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) {
                            result = onSwipeRight();
                        } else {
                            result = onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            result = onSwipeBottom();
                        } else {
                            result = onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public boolean onSwipeRight() {
        return false;
    }

    public boolean onSwipeLeft() {
        return false;
    }

    public boolean onSwipeTop() {
        return false;
    }

    public boolean onSwipeBottom() {
        return false;
    }
}

And sample usage:

    background.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            toggleSomething();
        }
    });
    background.setOnTouchListener(new OnSwipeTouchListener() {
        public boolean onSwipeTop() {
            Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
            return true;
        }
        public boolean onSwipeRight() {
            Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
            return true;
        }
        public boolean onSwipeLeft() {
            Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
            return true;
        }
        public boolean onSwipeBottom() {
            Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
Iodometry answered 28/1, 2013 at 22:37 Comment(6)
I don't see easily the modifications in your modified OnSwipeTouchListener. Where is it exactly ?Angioma
This should be the accepted answer... Differences are subtle but very important. First, there is no onDown(). Second, the handlers return a boolean to signal whether they consumed the event or not. This is of utmost importance if you need more than just one handler for the same view (which should be the default case, anyway).Spite
What's that toggleSomething() Method?Ng
@Ng just normal tap(not gesture) handlerIodometry
@NicolasZozol java code style, all swipe directions, on click handlerIodometry
@Iodometry "Thanks but in this case not initialized object can be root of NPE errors It's always safer to assign something not null to callback instance" The final modifier guarantees gestureDetector will always be initialized, and if not caught by the compiler. The edit I suggested to update to non-deprecated API is perfectly safe.Boatsman
F
32

Expanding on Mirek's answer, for the case when you want to use the swipe gestures inside a scroll view. By default the touch listener for the scroll view get disabled and therefore scroll action does not happen. In order to fix this you need to override the dispatchTouchEvent method of the Activity and return the inherited version of this method after you're done with your own listener.

In order to do a few modifications to Mirek's code: I add a getter for the gestureDetector in the OnSwipeTouchListener.

public GestureDetector getGestureDetector(){
    return  gestureDetector;
}

Declare the OnSwipeTouchListener inside the Activity as a class-wide field.

OnSwipeTouchListener onSwipeTouchListener;

Modify the usage code accordingly:

onSwipeTouchListener = 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();
    }
});

imageView.setOnTouchListener(onSwipeTouchListener);

And override the dispatchTouchEvent method inside Activity:

@Override
    public boolean dispatchTouchEvent(MotionEvent ev){
        swipeListener.getGestureDetector().onTouchEvent(ev); 
            return super.dispatchTouchEvent(ev);   
    }

Now both scroll and swipe actions should work.

Fadden answered 15/11, 2013 at 16:12 Comment(1)
a giant minus here - if I do a gesture anywhere on my scrollview, the touchListener is called, even if it wasn't applied to the scrollview itself, but to a random button inside itConsol
D
28

In order to have Click Listener, DoubleClick Listener, OnLongPress Listener, Swipe Left, Swipe Right, Swipe Up, Swipe Down on Single View you need to setOnTouchListener. i.e,

view.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {

            @Override
            public void onClick() {
                super.onClick();
                // your on click here
            }

            @Override
            public void onDoubleClick() {
                super.onDoubleClick();
                // your on onDoubleClick here
            }

            @Override
            public void onLongClick() {
                super.onLongClick();
                // your on onLongClick here
            }

            @Override
            public void onSwipeUp() {
                super.onSwipeUp();
                // your swipe up here
            }

            @Override
            public void onSwipeDown() {
                super.onSwipeDown();
                // your swipe down here.
            }

            @Override
            public void onSwipeLeft() {
                super.onSwipeLeft();
                // your swipe left here.
            }

            @Override
            public void onSwipeRight() {
                super.onSwipeRight();
                // your swipe right here.
            }
        });

}

For this you need OnSwipeTouchListener class that implements OnTouchListener.

public class OnSwipeTouchListener implements View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

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 onSingleTapUp(MotionEvent e) {
        onClick();
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        onDoubleClick();
        return super.onDoubleTap(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        onLongClick();
        super.onLongPress(e);
    }

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @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();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

public void onClick() {

}

public void onDoubleClick() {

}

public void onLongClick() {

}
}
Democratic answered 28/4, 2015 at 10:3 Comment(4)
Zala's solution is simple and clear, helped me greatly with swipe actions in android. This solution will solve beginners who have trouble with swipe gestures.Magma
@Jaydipsinh Zala, You save my time, amazing. this will solve my problem of indicator of scroll top and bottom while scrolling.Abshire
This is what I call a complete answer. When I added the swipes, I lost my click properties, so I needed to override the onClick method as in this answer. Thanks guy!Monsoon
@Jaydipsinh Zala Not sure what am I doing wrong, when I add the OnSwipeTouchListener to my webView, it removes the interaction with the website inside webview. Can you help please?Overmantel
L
22

Kotlin version of @Mirek Rusin is here:

OnSwipeTouchListener.kt :

open class OnSwipeTouchListener(ctx: Context) : OnTouchListener {

    private val gestureDetector: GestureDetector

    companion object {

        private val SWIPE_THRESHOLD = 100
        private val SWIPE_VELOCITY_THRESHOLD = 100
    }

    init {
        gestureDetector = GestureDetector(ctx, GestureListener())
    }

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

    private inner class GestureListener : SimpleOnGestureListener() {


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

        override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
            var 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()
                        } 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
        }


    }

    open fun onSwipeRight() {}

    open fun onSwipeLeft() {}

    open fun onSwipeTop() {}

    open fun onSwipeBottom() {}
}

Usage:

view.setOnTouchListener(object : OnSwipeTouchListener(context) {

    override fun onSwipeTop() {
        super.onSwipeTop()
    }

    override fun onSwipeBottom() {
        super.onSwipeBottom()
    }

    override fun onSwipeLeft() {
        super.onSwipeLeft()
    }

    override fun onSwipeRight() {
        super.onSwipeRight()
    }
})

the open keyword was the point for me...

Limey answered 15/12, 2018 at 9:50 Comment(4)
Why we need to make the class open ??Animal
In kotlin, we can replace Math.abs with abs() functionAnimal
I did convert to kotlin and I don't recommend it. MotionEvent in onFling(e1:.. is @NonNullable but I got this stupid issue on carshlytics "Fatal Exception: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.m.f, parameter e1" at com.mypackage.ui.widget.OnHorizontalSwipeListener$GestureListener.onFling(OnHorizontalSwipeListener.kt:33)Attending
@KPradeepKumarReddy Because otherwise you won't be able to create anonymous objects that extend it, also it's needed, so the event methods are also open, so you can override them in your object.Recumbent
M
18

You don't need complicated calculations. It can be done just by using OnGestureListener interface from GestureDetector class.

Inside onFling method you can detect all four directions like this:

MyGestureListener.java:

import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class MyGestureListener implements GestureDetector.OnGestureListener{

    private static final long VELOCITY_THRESHOLD = 3000;

    @Override
    public boolean onDown(final MotionEvent e){ return false; }

    @Override
    public void onShowPress(final MotionEvent e){ }

    @Override
    public boolean onSingleTapUp(final MotionEvent e){ return false; }

    @Override
    public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX,
                        final float distanceY){ return false; }

    @Override
    public void onLongPress(final MotionEvent e){ }

    @Override
    public boolean onFling(final MotionEvent e1, final MotionEvent e2,
                       final float velocityX,
                       final float velocityY){

        if(Math.abs(velocityX) < VELOCITY_THRESHOLD 
                    && Math.abs(velocityY) < VELOCITY_THRESHOLD){
            return false;//if the fling is not fast enough then it's just like drag
        }

        //if velocity in X direction is higher than velocity in Y direction,
        //then the fling is horizontal, else->vertical
        if(Math.abs(velocityX) > Math.abs(velocityY)){
            if(velocityX >= 0){
                Log.i("TAG", "swipe right");
            }else{//if velocityX is negative, then it's towards left
                Log.i("TAG", "swipe left");
            }
        }else{
            if(velocityY >= 0){
                Log.i("TAG", "swipe down");
            }else{
                Log.i("TAG", "swipe up");
            }
        }

        return true;
    }
}

usage:

GestureDetector mDetector = new GestureDetector(MainActivity.this, new MyGestureListener());

view.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(final View v, final MotionEvent event){
        return mDetector.onTouchEvent(event);
    }
});
Mcgowen answered 4/6, 2017 at 18:38 Comment(5)
sifted through all the garbage here, you are right, no need for over-complicating things - this one works perfectlyConsol
Should VELOCITY_THRESHOLD not be screen density dependent?Slice
@GerritBeuze No, Android system handles that. It sends the right values through velocityX and velocityY in onFling method. although you can experiment to see which value best suits your needs but the final number would be universal.Mcgowen
The answers here suppose the opposite: they are not scaled ? : #18812979Slice
@GerritBeuze It's because they are doing the math manually themselves by calculating the amount of pixels the finger has traveled and that's wrong because it depends on pixel density. you should only use velocity like I did, velocity is almost dpi independent.Mcgowen
M
10

Use SwipeListView and let it handle the gesture detection for you.

Screenshot

Mutz answered 23/10, 2013 at 10:27 Comment(0)
N
9

To add an onClick as well, here's what I did.

....
// in OnSwipeTouchListener class

private final class GestureListener extends SimpleOnGestureListener {

    .... // normal GestureListener  code

   @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        onClick(); // my method
        return super.onSingleTapConfirmed(e);
    }

} // end GestureListener class

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }

    public void onClick(){ 
    }


    // as normal
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
}

} // end OnSwipeTouchListener class

I'm using Fragments, so using getActivity() for context. This is how I implemented it - and it works.


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

            public void onClick(){
                Toast.makeText(getActivity(), "clicked", Toast.LENGTH_SHORT).show();
            }
        });
Nasopharynx answered 2/5, 2014 at 8:58 Comment(0)
S
6

Since this Question is somewhat old and very popular I tried to update the answer and improve it by combining multiple suggestions in the answers and comments.

  • Switch from Java to Kotlin and used some Kotlin Syntax Eyecandy (subjective)
  • Made onSwipe-funcitons return whether they consumed the event (ruX answer)
  • Lifted result assignment out of if statement and removed variable (returns directly)
  • Alternatives to detect only either vertical and horizontal swipes
  • Alternatives to detect only either up, down, right and left.
  • Renamed Top and Bottom to Up and Down for consistency (directions, not positions).

Swipe all four directions:

open class OnSwipeTouchListener(val context: Context?) : OnTouchListener {
    companion object {
        private const val SwipeThreshold = 100
        private const val SwipeVelocityThreshold = 100
    }

    private val gestureDetector = GestureDetector(context, GestureListener())

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

    open fun onSwipeRight(): Boolean { return false }
    open fun onSwipeLeft(): Boolean { return false }
    open fun onSwipeUp(): Boolean { return false }
    open fun onSwipeDown(): Boolean { return false }

    private inner class GestureListener : SimpleOnGestureListener() {
        override fun onDown(e: MotionEvent): Boolean {
            return true
        }

        override fun onFling(
            e1: MotionEvent,
            e2: MotionEvent,
            velocityX: Float,
            velocityY: Float,
        ): Boolean {
            try {
                val diffY = e2.y - e1.y
                val diffX = e2.x - e1.x

                if (abs(diffX) > abs(diffY)) {
                    if (abs(diffX) > SwipeThreshold && abs(velocityX) > SwipeVelocityThreshold) {
                        return when {
                            diffX > 0 -> onSwipeRight()
                            else -> onSwipeLeft()
                        }
                    }
                } else if (abs(diffY) > SwipeThreshold && abs(velocityY) > SwipeVelocityThreshold) {
                    return when {
                        diffY > 0 -> onSwipeDown()
                        else -> onSwipeUp()
                    }
                }
            } catch (exception: Exception) {
                exception.printStackTrace()
            }
            return false
        }
    }
}

Usage:

myView.setOnTouchListener(object : OnSwipeTouchListener(context) {
            // Implement any function you need:
            override fun onSwipeUp(): Boolean {
                Toast.makeText(context, "UP", Toast.LENGTH_SHORT).show()
                return true
            }

            override fun onSwipeLeft(): Boolean {
                Toast.makeText(context, "LEFT", Toast.LENGTH_SHORT).show()
                return true
            }
        })

Alternatives for onFling()

The alternatives remove a bit more code and reorder checks. The reorder is not necessary but I think it looks a bit neater and its easier to understand. The code is to be put in the try bracket. The usage stays the same, just implement the respective onSwipe functions

Horizontal

val diffY = e2.y - e1.y

return if (abs(velocityY) > SwipeVelocityThreshold) {
    when {
        diffY > SwipeThreshold -> onSwipeDown()
        diffY < -SwipeThreshold -> onSwipeUp()
        else -> false
    }
} else false

Vertical

val diffX = e2.x - e1.x

return if (abs(velocityX) > SwipeVelocityThreshold) {
    when {
        diffX > SwipeThreshold -> onSwipeRight()
        diffX < -SwipeThreshold -> onSwipeLeft()
        else -> false
    }
} else false

One direction

Example for "up"

val diffY = e2.y - e1.y

return when {
    diffY < -SwipeThreshold -> onSwipeUp()
    else -> false
}

Additional Notes: In (ruX answer) they discussed removing onDown to also detect an onClickListener for the same view. However, when I removed onDown, the class did not work anymore. I don't exactly know why. I looked into the code of GestureDetector but it is very complex and the consequences of the return value of onDown seems to have some implications. So I left it in. Also, you can just use SimpleOnGestureListener.onSingleTapUp() just like you did onFling and create a onClick() function that is called there.

Version Info:

  • compileSdkVersion 31
  • minSdkVersion 24
  • targetSdkVersion 31
Sparkie answered 17/6, 2022 at 18:38 Comment(1)
This is solid! I've improved it a bit. Didn't want to post yet another answer though. My implementationDraft
A
5

@Edward Brey's method works great. If someone would also like to copy & paste the imports for the OnSwipeTouchListener, here they are:

 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;
Azure answered 22/5, 2015 at 8:42 Comment(0)
P
4

A little modification of @Mirek Rusin answer and now you can detect multitouch swipes. This code is on Kotlin:

class OnSwipeTouchListener(ctx: Context, val onGesture: (gestureCode: Int) -> Unit) : OnTouchListener {

private val SWIPE_THRESHOLD = 200
private val SWIPE_VELOCITY_THRESHOLD = 200

private val gestureDetector: GestureDetector

var fingersCount = 0

fun resetFingers() {
    fingersCount = 0
}

init {
    gestureDetector = GestureDetector(ctx, GestureListener())
}

override fun onTouch(v: View, event: MotionEvent): Boolean {
    if (event.pointerCount > fingersCount) {
        fingersCount = event.pointerCount
    }
    return gestureDetector.onTouchEvent(event)
}

private inner class GestureListener : SimpleOnGestureListener() {

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

    override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
        var 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) {
                        val gesture = when (fingersCount) {
                            1 -> Gesture.SWIPE_RIGHT
                            2 -> Gesture.TWO_FINGER_SWIPE_RIGHT
                            3 -> Gesture.THREE_FINGER_SWIPE_RIGHT
                            else -> -1
                        }
                        if (gesture > 0) {
                            onGesture.invoke(gesture)
                        }
                    } else {
                        val gesture = when (fingersCount) {
                            1 -> Gesture.SWIPE_LEFT
                            2 -> Gesture.TWO_FINGER_SWIPE_LEFT
                            3 -> Gesture.THREE_FINGER_SWIPE_LEFT
                            else -> -1
                        }
                        if (gesture > 0) {
                            onGesture.invoke(gesture)
                        }
                    }
                    resetFingers()
                }
            } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    val gesture = when (fingersCount) {
                        1 ->  Gesture.SWIPE_DOWN
                        2 -> Gesture.TWO_FINGER_SWIPE_DOWN
                        3 -> Gesture.THREE_FINGER_SWIPE_DOWN
                        else -> -1
                    }
                    if (gesture > 0) {
                        onGesture.invoke(gesture)
                    }
                } else {
                    val gesture = when (fingersCount) {
                        1 ->  Gesture.SWIPE_UP
                        2 -> Gesture.TWO_FINGER_SWIPE_UP
                        3 -> Gesture.THREE_FINGER_SWIPE_UP
                        else -> -1
                    }
                    if (gesture > 0) {
                        onGesture.invoke(gesture)
                    }
                }
                resetFingers()
            }
            result = true

        } catch (exception: Exception) {
            exception.printStackTrace()
        }

        return result
    }
}}

Where Gesture.SWIPE_RIGHT and others are unique integer indentificator of gesture that I`m using to detect kind of gesture later in my activity:

rootView?.setOnTouchListener(OnSwipeTouchListener(this, {
    gesture -> log(Gesture.parseName(this, gesture))
}))

So you see gesture here is an integer variable that holds value I have passed before.

Pelvic answered 26/8, 2016 at 1:57 Comment(5)
Can someone please show me an example of how to use Kotlin to detect both left swipe's and a normal click on the same view.Multiflorous
How do you use this in a list view? It does not return the position of the view that was touched so how do you know which row was touched?Multiflorous
Gesture comes from what import? import android.gesture.Gesture does not have a SWIPE_RIGHT global.Sweepstakes
why you did not write down the complete code of this gesture, where to put SwipeDown, left, right etc?Derwood
Can we also detect tap gesture ?Animal
S
4

My solution is similar to those above but I have abstracted the gesture handling into an abstract class OnGestureRegisterListener.java, which includes swipe, click and long click gestures.

OnGestureRegisterListener.java

public abstract class OnGestureRegisterListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;
    private View view;

    public OnGestureRegisterListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        this.view = view;
        return gestureDetector.onTouchEvent(event);
    }

    public abstract void onSwipeRight(View view);
    public abstract void onSwipeLeft(View view);
    public abstract void onSwipeBottom(View view);
    public abstract void onSwipeTop(View view);
    public abstract void onClick(View view);
    public abstract boolean onLongClick(View view);

    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 void onLongPress(MotionEvent e) {
            onLongClick(view);
            super.onLongPress(e);
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            onClick(view);
            return super.onSingleTapUp(e);
        }

        @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(view);
                        } else {
                            onSwipeLeft(view);
                        }
                        result = true;
                    }
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom(view);
                    } else {
                        onSwipeTop(view);
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }

    }
}

And use it like so. Note that you can also easily pass in your View parameter.

OnGestureRegisterListener onGestureRegisterListener = new OnGestureRegisterListener(this) {
    public void onSwipeRight(View view) {
        // Do something
    }
    public void onSwipeLeft(View view) {
        // Do something
    }
    public void onSwipeBottom(View view) {
        // Do something
    }
    public void onSwipeTop(View view) {
        // Do something
    }
    public void onClick(View view) {
        // Do something
    }
    public boolean onLongClick(View view) { 
        // Do something
        return true;
    }
};

Button button = findViewById(R.id.my_button);
button.setOnTouchListener(onGestureRegisterListener);
Sine answered 2/8, 2018 at 8:28 Comment(1)
I don't quite see the use for abstraction here. Doesn't it just mandate that any inheriting class override every function? Meaning onSwipeLeft, onSwipeBottom etc. I'd say sometimes that is not necessary and only makes your code ugly.Sparkie
M
4

I've been doing similar things, but for horizontal swipes only

import android.content.Context
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View

abstract class OnHorizontalSwipeListener(val context: Context) : View.OnTouchListener {    

    companion object {
         const val SWIPE_MIN = 50
         const val SWIPE_VELOCITY_MIN = 100
    }

    private val detector = GestureDetector(context, GestureListener())

    override fun onTouch(view: View, event: MotionEvent) = detector.onTouchEvent(event)    

    abstract fun onRightSwipe()

    abstract fun onLeftSwipe()

    private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {    

        override fun onDown(e: MotionEvent) = true

        override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float)
            : Boolean {

            val deltaY = e2.y - e1.y
            val deltaX = e2.x - e1.x

            if (Math.abs(deltaX) < Math.abs(deltaY)) return false

            if (Math.abs(deltaX) < SWIPE_MIN
                    && Math.abs(velocityX) < SWIPE_VELOCITY_MIN) return false

            if (deltaX > 0) onRightSwipe() else onLeftSwipe()

            return true
        }
    }
}

And then it can be used for view components

private fun listenHorizontalSwipe(view: View) {
    view.setOnTouchListener(object : OnHorizontalSwipeListener(context!!) {
            override fun onRightSwipe() {
                Log.d(TAG, "Swipe right")
            }

            override fun onLeftSwipe() {
                Log.d(TAG, "Swipe left")
            }

        }
    )
}
Mullen answered 27/12, 2018 at 13:12 Comment(0)
G
3

This question was asked many years ago. Now, there is a better solution: SmartSwipe: https://github.com/luckybilly/SmartSwipe

code looks like this:

SmartSwipe.wrap(contentView)
        .addConsumer(new StayConsumer()) //contentView stay while swiping with StayConsumer
        .enableAllDirections() //enable directions as needed
        .addListener(new SimpleSwipeListener() {
            @Override
            public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
                //direction: 
                //  1: left
                //  2: right
                //  4: top
                //  8: bottom
            }
        })
;
Gitagitel answered 25/7, 2019 at 10:1 Comment(2)
There are many SwipeConsumers for different sideslip effects, such as SlidingConsumer/StretchConsumer/SpaceConsumer/... and so on within SmartSwipeGitagitel
this library was hosted in jcenter/bintray so since jcenter/bintray is no longer available to public since 2021, no bueno on this library.Geographer
S
1

@Mirek Rusin answeir is very good. But, there is small bug, and fix is requried -

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) {
                            if (getOnSwipeListener() != null) {
                                getOnSwipeListener().onSwipeRight();
                            }
                        } else {
                            if (getOnSwipeListener() != null) {
                                getOnSwipeListener().onSwipeLeft();
                            }
                        }
                        result = true;
                    }
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        if (getOnSwipeListener() != null) {
                            getOnSwipeListener().onSwipeBottom();
                        }
                    } else {
                        if (getOnSwipeListener() != null) {
                            getOnSwipeListener().onSwipeTop();
                        }
                    }
                    result = true;
                }

What the difference? We set result = true, only if we have checked that all requrinments (both SWIPE_THRESHOLD and SWIPE_VELOCITY_THRESHOLD are Ok ). This is important if we discard swipe if some of requrinments are not achieved, and we have to do smth in onTouchEvent method of OnSwipeTouchListener!

Slur answered 30/12, 2014 at 0:1 Comment(0)
M
1

Here is simple Android Code for detecting gesture direction

In MainActivity.java and activity_main.xml, write the following code:

MainActivity.java

import java.util.ArrayList;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.GestureStroke;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity implements
        OnGesturePerformedListener {

    GestureOverlayView gesture;
    GestureLibrary lib;
    ArrayList<Prediction> prediction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lib = GestureLibraries.fromRawResource(MainActivity.this,
                R.id.gestureOverlayView1);
        gesture = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
        gesture.addOnGesturePerformedListener(this);
    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<GestureStroke> strokeList = gesture.getStrokes();
        // prediction = lib.recognize(gesture);
        float f[] = strokeList.get(0).points;
        String str = "";

        if (f[0] < f[f.length - 2]) {
            str = "Right gesture";
        } else if (f[0] > f[f.length - 2]) {
            str = "Left gesture";
        } else {
            str = "no direction";
        }
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();

    }

}

activity_main.xml

<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    android:id="@+id/gestureOverlayView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android1:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw gesture"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</android.gesture.GestureOverlayView>
Magistral answered 6/6, 2016 at 6:45 Comment(0)
P
1

the usage of Edward Brey's answer in Kotlin

 view.setOnTouchListener(object: OnSwipeTouchListener(this) {
      override fun onSwipeLeft() {
        super.onSwipeLeft()
      }
      override fun onSwipeRight() {
        super.onSwipeRight()
      }
    }
 )
Penholder answered 19/6, 2017 at 2:22 Comment(0)
U
1

This issue still exists. Add the following classes:

private class SwipeFirstTouchListener implements View.OnTouchListener {

    private final DirtyOnSwipeTouchListener swipe;
    private final View.OnTouchListener delegate;

    private SwipeFirstTouchListener(DirtyOnSwipeTouchListener swipe, View.OnTouchListener delegate) {
        this.swipe = swipe;
        this.delegate = delegate;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (!swipe.onTouch(v, event)) {
            // no a swipe, so lets try with the rest of the events
            return delegate.onTouch(v, event);
        }
        return false;
    }
}

and

private class DirtyOnSwipeTouchListener extends OnSwipeTouchListener {
    private boolean dirty = false;
    private OnSwipeTouchListener delegate;

    public DirtyOnSwipeTouchListener(Context ctx, OnSwipeTouchListener delegate) {
        super(ctx);

        this.delegate = delegate;
    }

    private void reset() {
        dirty = false;
    }

    public void onSwipeTop() {
        dirty = true;

        delegate.onSwipeTop();
    }

    public void onSwipeRight() {
        dirty = true;
        delegate.onSwipeRight();
    }

    public void onSwipeLeft() {
        dirty = true;
        delegate.onSwipeLeft();
    }

    public void onSwipeBottom() {
        dirty = true;
        delegate.onSwipeBottom();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        try {
            super.onTouch(v, event);

            return dirty;
        } finally {
            dirty = false;
        }

    }
};

and a class found on the internet:

public class OnSwipeTouchListener implements OnTouchListener {
    
    private static final String TAG = OnSwipeTouchListener.class.getName();

    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) {
                Log.d(TAG, "Unexpected problem handling swipes, ignoring.", exception);
            }
            return result;
        }
    }

    public void onSwipeRight() {
        // Do nothing
    }

    public void onSwipeLeft() {
        // Do nothing
    }

    public void onSwipeTop() {
        // Do nothing
    }

    public void onSwipeBottom() {
        // Do nothing
    }
}

then add your own OnTouchListener and OnSwipeTouchListener like so:

    DirtyOnSwipeTouchListener swipe = new DirtyOnSwipeTouchListener(this, new OnSwipeTouchListener(this) {
        public void onSwipeTop() {
            // your code here
        }

        public void onSwipeRight() {
            // your code here
        }

        public void onSwipeLeft() {
            // your code here
        }

        public void onSwipeBottom() {
            // your code here
        }
    });


    View.OnTouchListener toggleListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // your code here

                return true;
            } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // your code here

                return true;
            }
            return false;
        }
    };

    SwipeFirstTouchListener swipeFirstTouchListener = new SwipeFirstTouchListener(swipe, toggleListener);

    myView.setOnTouchListener(swipeFirstTouchListener);
Urbane answered 17/8, 2020 at 10:13 Comment(2)
giving a complete sample on how to use the code would have been great!Heracliteanism
Added missing class and exapleUrbane
F
0

If you want to display some buttons with actions when an list item is swipe are a lot of libraries on the internet that have this behavior. I implemented the library that I found on the internet and I am very satisfied. It is very simple to use and very quick. I improved the original library and I added a new click listener for item click. Also I added font awesome library (http://fortawesome.github.io/Font-Awesome/) and now you can simply add a new item title and specify the icon name from font awesome.

Here is the github link

Flasher answered 24/1, 2015 at 14:30 Comment(0)
I
0
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

/**
 * Detects left and right swipes across a view.
 */
class OnSwipeTouchListener(context: Context, onSwipeCallBack: OnSwipeCallBack?) : OnTouchListener {

    private var gestureDetector : GestureDetector
    private var onSwipeCallBack: OnSwipeCallBack?=null

    init {

        gestureDetector = GestureDetector(context, GestureListener())
        this.onSwipeCallBack = onSwipeCallBack!!
    }
    companion object {

        private val SWIPE_DISTANCE_THRESHOLD = 100
        private val SWIPE_VELOCITY_THRESHOLD = 100
    }

   /* fun onSwipeLeft() {}

    fun onSwipeRight() {}*/

    override fun onTouch(v: View, event: MotionEvent): Boolean {


        return gestureDetector.onTouchEvent(event)
    }

    private inner class GestureListener : SimpleOnGestureListener() {

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

        override fun onFling(eve1: MotionEvent?, eve2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
            try {
                if(eve1 != null&& eve2!= null) {
                    val distanceX = eve2?.x - eve1?.x
                    val distanceY = eve2?.y - eve1?.y
                    if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (distanceX > 0)
                            onSwipeCallBack!!.onSwipeLeftCallback()
                        else
                            onSwipeCallBack!!.onSwipeRightCallback()
                        return true
                    }
                }
            }catch (exception:Exception){
                exception.printStackTrace()
            }

            return false
        }


    }
}
Insulin answered 13/6, 2018 at 11:39 Comment(4)
For Kotlin You can use something like this:Insulin
Usage:gv_calendar!!.setOnTouchListener(OnSwipeTouchListener(activity,onSwipeCallBack!!))Insulin
Please don't just dump code, include an explanation of what your code does.Uhl
what is OnSwipeCallBack?Ptarmigan
W
0

I know its a bit late since 2012 but I hope it will help someone since I think it's a shorter and cleaner code than most of the answers:

view.setOnTouchListener((v, event) -> {
        
int action = MotionEventCompat.getActionMasked(event);

switch(action) {
    case (MotionEvent.ACTION_DOWN) :
        Log.d(DEBUG_TAG,"Action was DOWN");
        return true;

    case (MotionEvent.ACTION_MOVE) :
        Log.d(DEBUG_TAG,"Action was MOVE");
        return true;

    case (MotionEvent.ACTION_UP) :
        Log.d(DEBUG_TAG,"Action was UP");
        return true;

    case (MotionEvent.ACTION_CANCEL) :
        Log.d(DEBUG_TAG,"Action was CANCEL");
        return true;

    case (MotionEvent.ACTION_OUTSIDE) :
        Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                "of current screen element");
        return true;

    default :
        return super.onTouchEvent(event);
}
    });

of course you can leave only the relevant gestures to you.

src: https://developer.android.com/training/gestures/detector

Worst answered 24/10, 2020 at 11:12 Comment(1)
From your own source: "This is the kind of processing you would have to do for a custom gesture. However, if your app uses common gestures such as double tap, long press, fling, and so on, you can take advantage of the GestureDetector class." So I would guess that the accepted approach might be right here. Not sure how these MotionEvent ACTIONS are supposed to work. Because while they look like they might are defined as complete gestures, the paragraph I just posted and the Doc suggests otherwise. Didnt test it though. So maybe some extra explanation would help.Sparkie

© 2022 - 2024 — McMap. All rights reserved.