Im having a calendar and I want to make it possible to swipe left/right and switch months.
Is there any listener to swipe left/right?
Thank you!
Asked
Active
Viewed 3,251 times
2

RE6
- 2,684
- 4
- 31
- 57
-
This question is answered in detail here: http://stackoverflow.com/questions/937313/android-basic-gesture-detection – Charles Munger Nov 25 '11 at 11:14
2 Answers
5
You can user a SimpleOnGestureListener
like the example below: (obviously, replace the toast with the actual code that move to next/previous month)
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 {
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(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
Then you attach it to your view:
// Do this for each view added to the grid
view.setOnClickListener(SelectFilterActivity.this);
view.setOnTouchListener(gestureListener);

Guillaume
- 22,694
- 14
- 56
- 70
-
It doesn't work. `gridview.setOnClickListener(SelectFilterActivity.this);` Error: `No enclosing instance of the type SelectFilterActivity is accessible in scope ` and another error `gridview.setOnTouchListener(gestureListener);` Error: `gestureListener cannot be resolved to a variable` – RE6 Nov 25 '11 at 11:29
-
Well you need to adapt it to your specific case, you didn't provide enough details to code the full solution. The important part is the MyGestureDetector class, that you need to somehow plug to your view – Guillaume Nov 25 '11 at 14:38
0
SimpleOngestureListener can catch onFling ; you can override this to catch swipes .
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
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) {
//do your stuff
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//do your stuff
}
} catch (Exception e)
{
// nothing
}
return false;
}

ron
- 159
- 2
- 11
-
We didn't ... ? but no thanks. i've been using this piece of code for a while now, apparantly so have others. – ron Nov 25 '11 at 12:42