Skip to content

Instantly share code, notes, and snippets.

@Rudis1261
Created May 30, 2017 07:15
Show Gist options
  • Save Rudis1261/299a4d51bfb9ffeff2206a37e09dcc6b to your computer and use it in GitHub Desktop.
Save Rudis1261/299a4d51bfb9ffeff2206a37e09dcc6b to your computer and use it in GitHub Desktop.
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
float deltaX = x2 - x1;
float deltaY = y2 - y1;
// Left / Right
if (Math.abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
Toast.makeText(this, "Left to Right swipe [Next]", Toast.LENGTH_SHORT).show ();
}
// Right to left swipe action
else
{
Toast.makeText(this, "Right to Left swipe [Previous]", Toast.LENGTH_SHORT).show ();
}
} else {
// consider as something else - a screen tap for example
}
// Up / Down
if (Math.abs(deltaY) > MIN_DISTANCE)
{
// Top to Bottom, (INVERSE IF REVERSED)
if (y2 > y1)
{
Toast.makeText(this, "Top to Bottom swipe [Next]", Toast.LENGTH_SHORT).show ();
}
// Bottom to Top
else {
Toast.makeText(this, "Bottom to Top swipe [Previous]", Toast.LENGTH_SHORT).show ();
}
} else {
// consider as something else - a screen tap for example
}
break;
}
return super.onTouchEvent(event);
}
@Rudis1261
Copy link
Author

Might need to reverse the logic on line:40 if the UP / DOWN is reversed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment