Android Simple Activity Animation

In this post i will explain in simple steps how to add some animation while switching between activities.

To start with first have a look at android developers animation resources HERE

After this lets start with actual code

File 1 left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
 android:fromYDelta="0%" android:toYDelta="0%"
 android:duration="500"/>
 </set>

File 2 right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true">
<translate android:fromXDelta="100%" android:toXDelta="0%"
 android:fromYDelta="0%" android:toYDelta="0%"
 android:duration="500"/>
</set>

Place these two files in res/anim folder

After this override the default activity transition by calling the following way

startActivity(new Intent(TestActivity.this,TestActivity.class));     TestActivity.this.finish();     overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
 

You can change the animation properties to have some different transitions say bottom to top or fade in fade out etc

Enjoy the stuff.

Advertisements