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);
}
}
}