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.
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.
Comments