How to change all the activity transitions at once in Android application?

I know I can change activity transition using the following code right after startActivity() or finish()

activity.overridePendingTransition(R.anim.activity_close_enter, R.anim.activity_close_exit);

But if I have ten activities in my app, I have to do that ten times; and it is quite hard to modify. So I'm wondering if there is a way to set transition for all activities within the application at once. Is there any corresponding configuration in AndroidManifest.xml?

Thanks!


You want to first create a <style> in res/styles.xml, like this:

    <style name="YourAnimation.Activity" parent="@android:style/Animation.Activity"> 
       <item name="android:windowEnterAnimation">@anim/your_in_down</item>
       <item name="android:windowExitAnimation">@anim/your_out_down</item>
    </style>

Then you can apply the style to a theme, in the same file:

    <style name="YourTheme" parent="android:Theme.Translucent">
       ...
       <item name="android:windowAnimationStyle">@style/YourAnimation.Activity</item>
    </style>

And finally apply the theme to your activities in the manifest:

    <activity
        android:name=".YourActivity"
        android:theme="@style/YourTheme" />

Look at these links for reference:

  • Android Reference - Apply A Theme
  • Android Reference - WindowEnterAnimation

I know this has been answered but here is what I did in mine. We still support API 14 so there are some animations missing that I had to pull into the project from API 22( slide_in_right, slide_out_left). What this does is to slide in the screens when you open a new activity and slides the closing one out to the left. When you press back it will then do the opposite, sliding from the left the previous screen and closing out to the right the current screen.

<style name="YourTheme" parent="android:Theme.Translucent">
   ...
    <item name="android:windowAnimationStyle">@style/YourAnimation.Activity</item>
</style>

<style name="YourAnimation.Activity" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseEnterAnimation">@android:anim/slide_in_left</item>
    <item name="android:activityCloseExitAnimation">@android:anim/slide_out_right</item>
</style>

My solution is mostly like JPM answer. But here is some additional file that you may require.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>

</style>

<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
</style>

Create anim folder under res folder and then create this four animation files:

slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

Step 1: Create one base activity

Step 2: Extend all your activity to this base activity

Step 3: In your base activity add following code

@Override
protected void onStart() {
super.onStart();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}

@Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
    overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
 }
}