48

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
  • 655
  • 1
  • 6
  • 7

4 Answers4

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,929
  • 24
  • 44
gianpi
  • 3,110
  • 1
  • 17
  • 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. – Etienne Lawlor 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. – Weiyi Jul 07 '16 at 07:05
  • This doesn't seem to work for me. Each page transitions with no animation. Help? – Marvin Oct 06 '16 at 19:14
24

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
  • 9,077
  • 13
  • 78
  • 137
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);
 }
}
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
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
  • 609
  • 8
  • 6