I'm trying to simulate the swipe-to-right gesture in Android with this method:
private void addGestures () {
final int[] x1 = {0};
final int[] x2 = {0};
final int[] y1 = {0};
final int[] y2 = {0};
final int[] t1 = {0};
final int[] t2 = {0};
View myView = findViewById(R.id.detail_activity);
myView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1[0] = (int)event.getX();
y1[0] = (int) event.getY();
t1[0] = (int) System.currentTimeMillis();
return true;
case MotionEvent.ACTION_UP:
x2[0] = (int) event.getX();
y2[0] = (int) event.getY();
t2[0] = (int) System.currentTimeMillis();
if (x2[0] > x1[0]) {
onBackPressed();
}
return true;
case MotionEvent.ACTION_MOVE:
return true;
}
return false;
}
});
}
The problem is that now, as soon as I touch the screen, it gets recognized as a Gesture and the onBackPressed method gets invoked. I added a third variable to check the time the user takes to touch the screen. How can I implement them to recognize the swipe?