1

In the below posted code, I am trying to detect swipes using onDown and onFling but when I run the App, nothing appears.

I expected to see output coming from onFling when I hold touching the screen and drag my finger across it quickly, but i received nothing. Also, I expected to see the output coming from onDown when I press on the screen, but as well, nothing showed up.

please let me know what I am missing Or I misundersatnd the functionalities of onFling on onDown?!

JavaCode:

super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_gesture_activivty);

    mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());

            mViewGestureDetector = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return mGestureDetector.onTouchEvent(event);
        }
    };
    final TextView view = (TextView) findViewById(R.id.accXLabel);      
}

private class MySwipeGestureDetector extends SimpleOnGestureListener implements OnTouchListener {

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onDown", Toast.LENGTH_LONG).show();
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return mGestureDetector.onTouchEvent(event);
    }

}

}

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • You're not adding the OnGestureListener to any view, as far the posted code shows (?) – matiash May 25 '14 at 18:07
  • will be edited shortly...posted wrong code – Amrmsmb May 25 '14 at 18:16
  • @Amr you have to register/link the view(s)"buttons, framelayout, relatives layouts,...etc) you are willing to detect a swipe across them, with an object of Gesturedetector class. I think you are missing this step –  May 25 '14 at 19:48

2 Answers2

1

A GestureDetector works by translating multiple MotionEvents into particular gestures. To do so, it must receive the input from a view's touch events.

You're simply missing

view.setOnTouchListener(mViewGestureDetector);

for a particular view (the one you want to catch flings on).

matiash
  • 54,791
  • 16
  • 125
  • 154
1

First, I assume for an example that, all your views in the layout.xml file are wrapped inside a framelayout and you want your swipes to be detectable across the entire framelayout. If so, register your framelayout to the object of the type GestureDetector as shown in the following code:

FrameLayout mFL = (FrameLayout) findViewById(R.id.framelayoutID);

    mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());
    mFL.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return mGestureDetector.onTouchEvent(event);
        }
    }); 

Regarding the simpleOnGestureListener, here is how to implement onFling. it works for me.:

//define these variables globally:
/* private static final int MIN_DIST = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOC = 200;*/
...
...
...
@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("SwipeGesture", "Left Swipe");
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("SwipeGesture", "Right Swipe");
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }       
}