Note that there are some explanatory texts on larger screens.

plurals
  1. POissue with screen swipe detection code
    primarykey
    data
    text
    <p>Can someone help me with the code below. I am using this code to detect finger swipe on the screen.</p> <pre><code>import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class ActivitySwipeDetector implements View.OnTouchListener { private Activity activity; static final int MIN_DISTANCE = 100; private float downX, downY, upX, upY; public ActivitySwipeDetector(final Activity activity) { this.activity = activity; } public void onRightToLeftSwipe() { } public void onLeftToRightSwipe(){ } public void onTopToBottomSwipe(){ } public void onBottomToTopSwipe(){ } public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: { Log.i("msg", "in MotionEvent.ACTION_DOWN "+event.getX()); downX = event.getX(); downY = event.getY(); } case MotionEvent.ACTION_UP: { Log.i("msg", "in MotionEvent.ACTION_UP "+event.getX()); upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; // swipe horizontal? if(Math.abs(deltaX) &gt; MIN_DISTANCE){ // left or right if(deltaX &lt; 0) { this.onLeftToRightSwipe(); return true; } if(deltaX &gt; 0) { this.onRightToLeftSwipe(); return true; } } else { Log.i("msg", "X-axis Swipe was only " + downX+"-"+upX + " long, need at least " + MIN_DISTANCE); } // swipe vertical? if(Math.abs(deltaY) &gt; MIN_DISTANCE){ // top or down if(deltaY &lt; 0) { this.onTopToBottomSwipe(); return true; } if(deltaY &gt; 0) { this.onBottomToTopSwipe(); return true; } } else { Log.i("msg", "Y-axis Swipe was only " + downY+"-"+upY + " long, need at least " + MIN_DISTANCE); } // return true; } } return false; } } </code></pre> <p>and this is how I am using this class in my code</p> <pre><code> myTextView.setOnTouchListener(new ActivitySwipeDetector(this) { public void onRightToLeftSwipe() { //do some stuff; } public void onLeftToRightSwipe() { //do some stuff; } public void onTopToBottomSwipe() { //do some stuff; } public void onBottomToTopSwipe() { //do some stuff; } }); </code></pre> <p>When I test this code(directly on phone, not on emulator) both the upX ad downX variables are returning same values. Same is the case with upY and downY values. And hence method onLeftToRightSwipe() etc are not being invoked in the first place.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload