Issue with Left to Right Swipe Gesture in Android

Varun Agarwal

When I pickup a call on my LG G6, I have to click on the centre of a green circle and scroll outward towards an outer circle which is accompanied by color changes for the call to be picked-up. I want to implement something similar in my app.

I have a button (300*20dp) which has an onTouchListener attached to it. On left to right swipe it triggers an event and sends a POST request to my server, else no request is sent.

What I currently have is

Button
    android:layout_marginTop="10dp"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:text="Swipe Right to Purchase!"
    android:id="@+id/btnSend"

My code in MainActivity is as follows

Button btnSend;
btnSend=(Button)findViewById(R.id.btnSend);

btnSend.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                myGestDetector.onTouchEvent(event);
                //Log.d(" VIEW : ",v.getLeft()+"  "+v.getRight());                    
                return true;
            }
        });
 myGestDetector = new GestureDetector(this, new 

GestureDetector.SimpleOnGestureListener()
        {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {    
            if (e2.getX()- e1.getX()>900) {
                Log.d(TAG, "Left to Right swipe performed");
                btnSend.setText("Order has been placed!");

            }else btnSend.setText("Swipe More!");
            return true;
        }

        @Override
        public boolean onDown(MotionEvent e1)
        {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
            return true;
        }
    });

This ensures that user has to swipe a substantial amount horizontally. For now I have put 900 by 0.75*(view.getRight-view.getLeft). If i swipe for lesser amount, the text changes to "swipe more" as expected but if i swipe over 900 then in the log i get

08-12 16:09:20.521 25598-25598/com.brandlabs.varun.yocity D/Motion is﹕ Left to Right swipe performed

This comes multiple times for a single swipe which is causing more than one POST request to be sent. My questions are

1) How can I stop this/ limit it to one event? If i de-comment this Log.d(" VIEW : ",v.getLeft()+" "+v.getRight()); then as well for every swipe I get multiple of these lines in the log.

2) Why is this happening?

3) When using this, as the user moves his finger, can I track it during action_move and change color of the button. Something like showing progress.

frogatto

Why is this happening?

This is happening because while you are swiping the screen once you have reached to the threshold 900, you will not lift up your finger at exactly the difference of 900. It is obvious that your finger goes further, for example, of differences 901, 902, ... . And each of which would satisfy that if-condition and results in another log in your logcat.

For fixing this issue change your GestureDetector class to

myGestDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
    boolean swipePerformed = true;

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if(!swipePerformed){    
            if (e2.getX()- e1.getX()>900) {
                Log.d(TAG, "Left to Right swipe performed");
                btnSend.setText("Order has been placed!");
                swipePerformed = true;
            } else btnSend.setText("Swipe More!");
            return true;
        }
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e1)
    {
        swipePerformed = false;
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        return false;
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swipe imageview left and right in android

From Dev

bx-slider swipe left and right gesture support

From Dev

Swipe Gesture issue

From Dev

Determine which VIEW in multiple views has gesture (double click, swipe left, swipe right, etc)?

From Java

Android: How to handle right to left swipe gestures

From Dev

Tabs Swipe direction in Right to Left Android app

From Dev

Android ListView Swipe Right and Left to Accept and Reject

From Dev

Swipe gesture in phonegap android

From Dev

Swipe gesture in phonegap android

From Dev

Swipe left right for poll

From Dev

Swipe left and right in scrollView

From Dev

How to update BindingContext of Grid inside swipe left/right gesture in custom renderer?

From Dev

How implement left/ right swipe/fling on layout in android

From Dev

Android - Show/Hide TextView using Swipe Left to Right

From Dev

VideoView with Gesture(Swipe function) in android

From Dev

Android Unlock like Swipe Gesture

From Dev

VideoView with Gesture(Swipe function) in android

From Dev

How to perform a segue with a swipe right gesture

From Dev

iOS: right swipe gesture not responding in UIPageViewController

From Dev

Xamarin Forms Swipe Left/Swipe Right Gestures

From Dev

Issue with Swipe Gesture Control in iOS7

From Dev

Determine whether a swipe event is for a left swipe or a right swipe

From Dev

Swift : setup a Pan Gesture left to right

From Java

disable the swipe gesture that opens the navigation drawer in android

From Dev

Appium swipe gesture not working in a list in Android

From Dev

Views Navigation Using Swipe Gesture android

From Dev

Appium swipe gesture not working in a list in Android

From Java

How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?

From Dev

Swipe animation from left to right infinite time

Related Related

  1. 1

    Swipe imageview left and right in android

  2. 2

    bx-slider swipe left and right gesture support

  3. 3

    Swipe Gesture issue

  4. 4

    Determine which VIEW in multiple views has gesture (double click, swipe left, swipe right, etc)?

  5. 5

    Android: How to handle right to left swipe gestures

  6. 6

    Tabs Swipe direction in Right to Left Android app

  7. 7

    Android ListView Swipe Right and Left to Accept and Reject

  8. 8

    Swipe gesture in phonegap android

  9. 9

    Swipe gesture in phonegap android

  10. 10

    Swipe left right for poll

  11. 11

    Swipe left and right in scrollView

  12. 12

    How to update BindingContext of Grid inside swipe left/right gesture in custom renderer?

  13. 13

    How implement left/ right swipe/fling on layout in android

  14. 14

    Android - Show/Hide TextView using Swipe Left to Right

  15. 15

    VideoView with Gesture(Swipe function) in android

  16. 16

    Android Unlock like Swipe Gesture

  17. 17

    VideoView with Gesture(Swipe function) in android

  18. 18

    How to perform a segue with a swipe right gesture

  19. 19

    iOS: right swipe gesture not responding in UIPageViewController

  20. 20

    Xamarin Forms Swipe Left/Swipe Right Gestures

  21. 21

    Issue with Swipe Gesture Control in iOS7

  22. 22

    Determine whether a swipe event is for a left swipe or a right swipe

  23. 23

    Swift : setup a Pan Gesture left to right

  24. 24

    disable the swipe gesture that opens the navigation drawer in android

  25. 25

    Appium swipe gesture not working in a list in Android

  26. 26

    Views Navigation Using Swipe Gesture android

  27. 27

    Appium swipe gesture not working in a list in Android

  28. 28

    How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?

  29. 29

    Swipe animation from left to right infinite time

HotTag

Archive