Can I change the Android startActivity() transition animation?

151,152

Solution 1

In the same statement in which you execute finish(), execute your animation there too. Then, in the new activity, run another animation. See this code:

fadein.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fillAfter="true">
     <alpha android:fromAlpha="1.0" 
            android:toAlpha="0.0"
            android:duration="500"/> //Time in milliseconds
</set>

In your finish-class

private void finishTask() {
    if("blabbla".equals("blablabla"){
        finish();
        runFadeInAnimation();
    }
}

private void runFadeInAnimation() {
    Animation a = AnimationUtils.loadAnimation(this, R.anim.fadein);
    a.reset();
    LinearLayout ll = (LinearLayout) findViewById(R.id.yourviewhere);
    ll.clearAnimation();
    ll.startAnimation(a);   
}

fadeout.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
           android:fillAfter="true">
  <alpha android:fromAlpha="0.0"
         android:toAlpha="1.0"
         android:duration="500"/>
</set>

In your new Activity-class you create a similiar method like the runFadeAnimation I wrote and then you run it in onCreate and don't forget to change the resources id to fadeout.

Solution 2

Starting from API level 5 you can call overridePendingTransition immediately to specify an explicit transition animation:

startActivity();
overridePendingTransition(R.anim.hold, R.anim.fade_in);

or

finish();
overridePendingTransition(R.anim.hold, R.anim.fade_out);

Solution 3

See themes on android: http://developer.android.com/guide/topics/ui/themes.html.

Under themes.xml there should be android:windowAnimationStyle where you can see the declaration of the style in styles.xml.

Example implementation:

<style name="AppTheme" parent="...">

    ...

    <item name="android:windowAnimationStyle">@style/WindowAnimationStyle</item>

</style>

<style name="WindowAnimationStyle">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>

Solution 4

Use overridePendingTransition

startActivity();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);

fadein.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set>

fadeout.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/anticipate_interpolator"
        android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>

Solution 5

For fadeIn and fadeOut, only add this after super.onCreate(savedInstanceState) in your new Activity class. You don't need to create something else (No XML, no anim folder, no extra function).

overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
Share:
151,152
coneybeare
Author by

coneybeare

http://matt.coneybeare.me @coneybeare I have had a ton iOS apps under my name and my business name. I have multiple client apps, and one from an established startup. Many of my apps have been featured by Apple. I developed two iOS apps that made it into the top 100. One of the apps held the number two spot for three weeks. Another floats in/out of the paid News top 10. My apps have over 10 million downloads. I do a ton of web work too using rails for all my sites. I do much of my own design for all the sites I have created.

Updated on December 04, 2021

Comments

  • coneybeare
    coneybeare over 2 years

    I am starting an activity and would rather have a alpha fade-in for startActivity(), and a fade-out for the finish(). How can I go about this in the Android SDK?

  • RightHandedMonkey
    RightHandedMonkey over 10 years
    Add something like: @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.hold, R.anim.fade_out); } for adding back animations.
  • elimirks
    elimirks over 10 years
    In addition, it may be better to use the default short animation time: android:duration="@android:integer/config_shortAnimTime"
  • Gelldur
    Gelldur almost 10 years
  • cy198706
    cy198706 almost 9 years
    This should be the best clean answer.
  • vovahost
    vovahost over 8 years
  • Choletski
    Choletski about 8 years
    where is runFadeAnimation()
  • Itiel Maimon
    Itiel Maimon over 6 years
    @RightHandedMonkey For adding back animations better override the finish(); method of the activity for case in which the activity ends by something else than the back button (e.g. a custom exit button...).
  • Android developer
    Android developer about 6 years
    Guys, please be sure to see the real answer below this post.
  • Farid Z
    Farid Z almost 6 years
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
  • Abhishek kumar
    Abhishek kumar over 5 years
    Best Solution .
  • Ali Khaki
    Ali Khaki over 5 years
    what is blabbla ??!!
  • Tamoxin
    Tamoxin about 5 years
    How would you access @android:anim/fade_in from the java code?