This is a very common effect, however the Android framework only provides the slide in animation. This is how I implement all the sliding transition animation. There’s in total 4 XMLs to define the animation, and with the use of overridePendingTransition() in appropriate location you can implement this easily.
pull_in_left.xml
1 2 3 4 5 6 7 | <?xml version=“1.0” encoding=“utf-8”?> <translate xmlns:android=“http://schemas.android.com/apk/res/android” android:duration=“300” android:fromXDelta=“-100%” android:toXDelta=“0%” android:interpolator=“@android:anim/decelerate_interpolator” /> |
pull_in_right.xml
1 2 3 4 5 6 7 | <?xml version=“1.0” encoding=“utf-8”?> <translate xmlns:android=“http://schemas.android.com/apk/res/android” android:duration=“300” android:fromXDelta=“100%” android:toXDelta=“0%” android:interpolator=“@android:anim/decelerate_interpolator” /> |
push_out_left.xml
1 2 3 4 5 6 | <?xml version=“1.0” encoding=“utf-8”?> <translate xmlns:android=“http://schemas.android.com/apk/res/android” android:duration=“300” android:fromXDelta=“0%” android:interpolator=“@android:anim/decelerate_interpolator” android:toXDelta=“-100%” /> |
push_out_right.xml
1 2 3 4 5 6 | <?xml version=“1.0” encoding=“utf-8”?> <translate xmlns:android=“http://schemas.android.com/apk/res/android” android:duration=“300” android:fromXDelta=“0%” android:interpolator=“@android:anim/decelerate_interpolator” android:toXDelta=“100%” /> |
Usage
When starting new Activity
add overridePendingTransition right after you call startActivity
1 2 3 | Intent intent = new Intent(mContext, NextActivity.class); startActivity(intent); overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right); |
When Back to previous Activity
When the user click back button, you will have to play the reserve animation to go back to previous Activity, you can do so by overriding the
onBackPressed method
<!– Crayon Syntax Highlighter v_2.7 losartan dosage.2_beta –>
1 2 3 4 5 | @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left); } |
Update 3 July 2015:
You can go to this GitHub repo to download the 4 xml files. I may add more animations there, make sure to star it!
16 replies on “Android Activity transition slide in/out animation”
Thank you very much!
Great information!
Nice work. but for going back to previous activity use
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
Thank you! It is nice..
Thanks for sharing.
Good job…
what is throw an error
Great Work. Simple, clean and cool
Thanx very easy method
Well done, Keep it up
thanks a lot,
how to screen spit and move from centre.
its working oppositely.
Thank you so much, Android web wasn’t provide the details.
Thanks. Here is my transition:
startActivity():
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
onBackPressed():
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
This is working code but not move with finger touch
very good thanks
Did you figure out a way to avoid appbar from animating during these animations?