Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had run into this problem at a hackathon. There's no way you can solve it with GestureOverlayView, so you have to use motion events. GestureOverlayView however works with slightly more "detailed" gestures, like a circle. What I've noticed is that GestureOverlayView best works for closed figures. So, motionEvent sensor it is. I found the code <a href="https://stackoverflow.com/questions/937313/android-basic-gesture-detection/938657#938657">here</a>. I modified Gav's answer for vertical swipes too. Haven't tested it though, but positive that it should work.</p> <p>Here's an example:</p> <pre><code>public class SelectFilterActivity extends Activity implements OnClickListener { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; private GestureDetector gestureDetector; View.OnTouchListener gestureListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* ... */ // Gesture detection gestureDetector = new GestureDetector(new MyGestureDetector()); gestureListener = new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }; } class MyGestureDetector extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { // downward swipe if (Math.abs(e1.getY() - e2.getY()) &gt; SWIPE_MAX_OFF_PATH &amp;&amp; Math.abs(velocityY) &gt; SWIPE_THRESHOLD_VELOCITY) Toast.makeText(SelectFilterActivity.this, "Downward Swipe", Toast.LENGTH_SHORT).show(); else if (Math.abs(e2.getY() - e1.getY()) &gt; SWIPE_MAX_OFF_PATH &amp;&amp; Math.abs(velocityY) &gt; SWIPE_THRESHOLD_VELOCITY) Toast.makeText(SelectFilterActivity.this, "Upward Swipe", Toast.LENGTH_SHORT).show(); // right to left swipe else if(e1.getX() - e2.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show(); } else if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { // nothing } return false; } } </code></pre> <p>Now add it to the views where you'd like gesture detection,</p> <pre><code>// Do this for each view added to the grid imageView.setOnClickListener(SelectFilterActivity.this); imageView.setOnTouchListener(gestureListener); </code></pre> <p>Cheers to <a href="https://stackoverflow.com/users/111734/gav">Gav</a> for the code!</p>
 

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