Start Activity with an animation

38,800

I am using this in a current project of mine, it is basically pretty simple. You define a new animation style in your styles.xml, like this:

<!-- just defines top layer "Animation" -->
<style name="Animation" />

<!-- the animations must have been defined in your "anim" folder, of course -->
<style name="Animation.MyAwesomeAnimation" parent="android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/myawesomeanimation_enter</item>
    <item name="android:activityOpenExitAnimation">@anim/hold_long</item>
    <item name="android:activityCloseEnterAnimation">@anim/hold_long</item>
    <item name="android:activityCloseExitAnimation">@anim/myawesomeanimation_exit</item>
</style>

Then set this style in a theme (themes.xml):

<style name="Theme.MyAwesomeTheme" parent="Theme.Default">
    <item name="android:windowAnimationStyle">@style/Animation.MyAwesomeAnimation</item>
</style>

And then you can simply set these themes to every activity you like in your AndroidManifest.xml:

<activity
    android:name=".MyAwesomeActivity"
    android:theme="@style/Theme.MyAwesomeTheme" />

Now I wish you big fun with activity animations! :-D

Share:
38,800
adityad
Author by

adityad

Software Engineer

Updated on July 09, 2022

Comments

  • adityad
    adityad almost 2 years

    I am trying to start an activity with a custom transition animation. The only way I have found out so far to do this (without using onPendingTransition() in the previous activity) is to use a custom theme on the activity and define either activityOpenEnterAnimation, taskOpenEnterAnimation, windowEnterAnimation or windowAnimationStyle to set the animation. But, none of these attributes are working for me. Some experimentation yielded the following results-

    If I set the windowAnimationStyle attribute to some custom style which defines values for activityOpenEnterAnimation, taskOpenEnterAnimation, windowEnterAnimation or windowAnimationStyle I can get rid of the default transition animation occurring at the start of the activity. It doesn't show the transition animation using the actual value specified but at least the default animation is not shown.

    According to the reference doc here,

    I should be able to define an animation at the start of the activity using activityOpenEnterAnimation. But no success so far.

    Any ideas?

  • Informatic0re
    Informatic0re almost 13 years
    After 4 houres of experimenting i have a great information for all!: It is realy important to activate the Animations for your phone: Settings->Display->Animation and select "all animations"!
  • devconsole
    devconsole over 12 years
    In my experience one should also derive from the default animations: <style name="Animation.MyAwesomeAnimation" parent="android:style/Animation.Activity">. Otherwise you remove the animations for launching the app from the home screen, etc.
  • Chris.Jenkins
    Chris.Jenkins about 11 years
    This is most defiantly an Android Pro Tip, I'm telling everyone about this.
  • sergey.n
    sergey.n over 9 years
    Also you should add android:windowEnterAnimation the same as android:activityOpenEnterAnimation to your style