[android] Can I change the Android startActivity() transition animation?

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?

This question is related to android animation fade transition

The answer is


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

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

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>

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.


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

 // 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();
  tartActivity(myIntent1, bndlanimation1);

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

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>

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>

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(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(activityCloseEnterAnimation, activityCloseExitAnimation))
    val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
    val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
    activityStyle.recycle()
    overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
}

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to animation

Simple CSS Animation Loop – Fading In & Out "Loading" Text Slidedown and slideup layout with animation How to have css3 animation to loop forever jQuery animated number counter from zero to value CSS Auto hide elements after 5 seconds Fragment transaction animation: slide in and slide out Android Animation Alpha Show and hide a View with a slide up/down animation Controlling fps with requestAnimationFrame? powerpoint loop a series of animation

Examples related to fade

Fade In on Scroll Down, Fade Out on Scroll Up - based on element position in window Fade Effect on Link Hover? How to fade changing background image Can I change the Android startActivity() transition animation? CSS3 Fade Effect How do you fade in/out a background color using jquery?

Examples related to transition

CSS Auto hide elements after 5 seconds CSS3 transition on click using pure CSS css transition opacity fade background Can I change the Android startActivity() transition animation? Activity transition in Android