Can I change the Android startActivity() transition animation?

AndroidAnimationFadeTransition

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 2 - Android

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 3 - Android

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 4 - Android

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 - Android

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);

Solution 6 - Android

If you always want to the same transition animation for the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    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);
    }
}

Solution 7 - Android

You can simply create a context and do something like below:-

private Context context = this;

And your animation:-

((Activity) context).overridePendingTransition(R.anim.abc_slide_in_bottom,R.anim.abc_slide_out_bottom);

You can use any animation you want.

Solution 8 - Android

I wanted to use the styles.xml solution, but it did not work for me with activities. Turns out that instead of using android:windowEnterAnimation and android:windowExitAnimation, I need to use the activity animations like this:

<style name="ActivityAnimation.Vertical" parent="">
    <item name="android:activityOpenEnterAnimation">@anim/enter_from_bottom</item>
    <item name="android:activityOpenExitAnimation">@anim/exit_to_bottom</item>
    <item name="android:activityCloseEnterAnimation">@anim/enter_from_bottom</item>
    <item name="android:activityCloseExitAnimation">@anim/exit_to_bottom</item>
    <item name="android:windowEnterAnimation">@anim/enter_from_bottom</item>
    <item name="android:windowExitAnimation">@anim/exit_to_bottom</item>
</style>

Which I then reference in my theme:

<style name="AppTheme">
    ...
    <item name="android:windowAnimationStyle">@style/ActivityAnimation.Vertical</item>
    ...
</style>

Also, for some reason this only worked from Android 8 and above. I added the following code to my BaseActivity, to fix it for the API levels below:

override fun finish() {
    super.finish()
    setAnimationsFix()
}

/**
 * The activityCloseExitAnimation and activityCloseEnterAnimation properties do not work correctly when applied from the theme.
 * So in this fix, we retrieve them from the theme, and apply them.
 * @suppress Incorrect warning: https://stackoverflow.com/a/36263900/1395437
 */
@SuppressLint("ResourceType")
private fun setAnimationsFix() {
    // Retrieve the animations set in the theme applied to this activity in the manifest..
    var activityStyle = theme.obtainStyledAttributes(intArrayOf(android.R.attr.windowAnimationStyle))
    val windowAnimationStyleResId = activityStyle.getResourceId(0, 0)
    activityStyle.recycle()
    // Now retrieve the resource ids of the actual animations used in the animation style pointed to by
    // the window animation resource id.
    activityStyle = theme.obtainStyledAttributes(windowAnimationStyleResId, intArrayOf(android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation))
    val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
    val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
    activityStyle.recycle()
    overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
}

Solution 9 - Android

 // CREATE anim 

 // CREATE animation,animation2  xml // animation like fade out 

  Intent myIntent1 = new Intent(getApplicationContext(), Attend.class);
  Bundle bndlanimation1 =  ActivityOptions.makeCustomAnimation(getApplicationContext(), 
  R.anim.animation, R.anim.animation2).toBundle();
  startActivity(myIntent1, bndlanimation1);

Solution 10 - Android

Most of the answers are pretty correct, but some of them are deprecated such as when using R.anim.hold and some of them are just elaboratig the process.

So, you can use:

startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionconeybeareView Question on Stackoverflow
Solution 1 - AndroidAllen ChanView Answer on Stackoverflow
Solution 2 - AndroidmonmonjaView Answer on Stackoverflow
Solution 3 - AndroidCurtainView Answer on Stackoverflow
Solution 4 - AndroidMohsen mokhtariView Answer on Stackoverflow
Solution 5 - AndroidCedrigaView Answer on Stackoverflow
Solution 6 - AndroidFarid ZView Answer on Stackoverflow
Solution 7 - AndroidJulfikarView Answer on Stackoverflow
Solution 8 - AndroidDaniel ZolnaiView Answer on Stackoverflow
Solution 9 - AndroidAshifView Answer on Stackoverflow
Solution 10 - AndroidRawnak YazdaniView Answer on Stackoverflow