Change Activity By Right or Left Swipe Android Studio With Source Code.

Опубликовано: 18 Март 2026
на канале: Alan Ranjoni
23,498
258

This Tutorial was improved to show a better demo of this simple code from the last tutorial and include a right swipe as well:

Steps:

Create your app as usual, then go to your MainActivity.java.
Declare variables x1, x2, y1, and y2.

Put the following onTouchEvent code right after your onCreate code block closing bracket.

public boolean onTouchEvent(MotionEvent touchEvent){
switch(touchEvent.getAction()){
case MotionEvent.ACTION_DOWN:
x1 = touchEvent.getX();
y1 = touchEvent.getY();
break;
case MotionEvent.ACTION_UP:
x2 = touchEvent.getX();
y2 = touchEvent.getY();
if(x1 less than x2){
Intent i = new Intent(MainActivity.this, SwipeLeft.class);
startActivity(i);
}else if(x1 greater than x2){
Intent i = new Intent(MainActivity.this, SwipeRight.class);
startActivity(i);
}
break;
}
return false;
}

Then add SwipeLeft and SwipeRight empty activities and make your own changes.
-----------------------------------------
||| You are Done! |||
-----------------------------------------
Good Luck

For a slow video with more explanation, check out this one
   • Swipe Between Activities Android Studio