46

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!

Gen Liu
  • 635
  • 1
  • 6
  • 7

5 Answers5

64

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:

Brian Deragon
  • 2,885
  • 22
  • 44
gianpi
  • 3,030
  • 1
  • 15
  • 13
  • 29
    It works! Actually, I change a little bit: `` – Gen Liu Nov 30 '11 at 06:10
  • 1
    @DavidLiu, the open animations look like they are working but the close animations dont take effect. – toobsco42 Apr 04 '14 at 17:39
  • @GenLiu can you show me your xml examples? I just cant see the difference between openexit and close enter, for ex. – Renan Bandeira Jan 09 '15 at 03:15
  • Thanks for the great answer, helped a lot! – JPM Aug 12 '15 at 21:28
  • Works for me, but I have to delete `parent="android:Theme.Translucent"`, otherwise it will not work. Thanks. – li2 Jul 07 '16 at 07:05
  • This doesn't seem to work for me. Each page transitions with no animation. Help? – AeroEchelon Oct 06 '16 at 19:14
23

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>
JPM
  • 8,360
  • 13
  • 73
  • 124
11

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>
Shohan Ahmed Sijan
  • 3,775
  • 1
  • 28
  • 37
  • this does not work at all, at least not on android 6 (huawei p8) – qkx Mar 06 '17 at 11:18
  • Its smoothly working in my huawei gPlay mini (Android 6) & Nexus 9(Android 7). Please run this project: https://github.com/ShohanAhmed/Android-Activity-Transition-Custom-Animation – Shohan Ahmed Sijan Mar 07 '17 at 05:01
  • i investigated an issue, and you are partially right. It is running also on my phone, but only when you call intent (to show new activity) from activity. If you want to open some activity from widget and use Pending Intent, it is not working there. In this case the only option is to set animations programatically in code like I originally mentioned.... – qkx Mar 08 '17 at 07:42
2

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);
 }
}
Ranjith Kumar
  • 13,385
  • 9
  • 95
  • 126
0
My solution is mostly like of others...
 <style name="YourAnimation.Activity" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">@anim/slidefromright</item>
        <item name="android:windowExitAnimation">@anim/slidetoright</item>
    </style>
<resources>

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


</resources>
Sahil Bansal
  • 207
  • 3
  • 3