title: Android: detecting fling gestures
body: <p>In my app I try to capture a fling event on a view (a <code>ScrollView</code> to be exact, but tried with <code>LinearLayout</code> as well). </p>
  
  <p>By setting breakpoints I can see the that the <code>MotionEvents</code> happen correctly up to the point where the <code>onFling()</code> should fire. </p>
  
  <p>The event flow is as follows:</p>
  
  <ol>
  <li>The view's <code>onTouchEvent</code></li>
  <li>The <code>OnGestureListener's onDown</code></li>
  <li>The <code>OnGestureListener's onShowPress</code></li>
  <li>The <code>OnGestureListener's onLongPress</code></li>
  </ol>
  
  <p>I'm at a loss on how to go on trying to debug something like that and Google search does not turn up much. Any pointers ?</p>
  
  <p>I also attach the relevant view that should handle the gestures:</p>
  
  <pre><code>import android.content.Context;
  import android.content.Intent;
  import android.view.GestureDetector;
  import android.view.MotionEvent;
  import android.widget.ScrollView;
  
  public class GestureScrollView extends ScrollView {
      private static int MAJOR_MOVE = 60;
      private GestureDetector gd;
  
      public GestureScrollView(final Context context) {
          super(context);
          gd = new GestureDetector(context,
                  new GestureDetector.OnGestureListener() {
  
                      @Override
                      public boolean onSingleTapUp(MotionEvent e) {
                          // TODO Auto-generated method stub
                          return false;
                      }
  
                      @Override
                      public void onShowPress(MotionEvent e) {
                          // TODO Auto-generated method stub
  
                      }
  
                      @Override
                      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                              float distanceY) {
                          // TODO Auto-generated method stub
                          return false;
                      }
  
                      @Override
                      public void onLongPress(MotionEvent e) {
                          // TODO Auto-generated method stub
  
                      }
  
                      @Override
                      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                              float velocityY) {
                          int dx = (int) (e2.getX() - e1.getX());
                          if (Math.abs(dx) &gt; MAJOR_MOVE
                                  &amp;&amp; Math.abs(velocityX) &gt; Math.abs(velocityY)) {
                              if (velocityX &lt; 0) {
                                  Intent intent = new Intent(context, Main.class);
                                  context.startActivity(intent);
                              }
                          }
                          return false;
                      }
  
                      @Override
                      public boolean onDown(MotionEvent e) {
                          // TODO Auto-generated method stub
                          return false;
                      }
                  });
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          return gd.onTouchEvent(event);
      }
  }
  </code></pre>