97

I am codifiying a transition effect between my logo activity and my Main activity, but I have the problem that before vanish the activity move to top:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" >
    </alpha>

</set>

How could I improve this code to get only a vanish effect?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
MarcForn
  • 3,321
  • 7
  • 25
  • 39

4 Answers4

261

You could create your own .xml animation files to fade in a new Activity and fade out the current Activity:

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<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" />

fade_out.xml

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

Use it in code like that: (Inside your Activity)

Intent i = new Intent(this, NewlyStartedActivity.class);
startActivity(i);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

The above code will fade out the currently active Activity and fade in the newly started Activity resulting in a smooth transition.

UPDATE: @Dan J pointed out that using the built in Android animations improves performance, which I indeed found to be the case after doing some testing. If you prefer working with the built in animations, use:

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

Notice me referencing android.R instead of R to access the resource id.

UPDATE: It is now common practice to perform transitions using the Transition class introduced in API level 19.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • 67
    Using the built in Android animations seems to result in a smoother transition: `overridePendingTransition(android.R.anim.fadein, android.R.anim.fadeout);` Viewing those files may also give you hints on how to improve your custom animations (e.g. by making the fade in last longer than the fade out). – Dan J Jan 17 '14 at 21:44
  • 42
    It has an undescore: `overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);` :) – AlvaroSantisteban Apr 30 '14 at 10:15
  • Do I have to use overridePendingTransition onCreate or in every Intent? Or depends on what I want? Thanks. – Ricardo Jan 12 '15 at 15:58
  • 19
    There's a option without "overriding": `Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(getContext(), android.R.anim.fade_in, android.R.anim.fade_out).toBundle(); startActivity(intent, bundle);` – oleynikd Oct 20 '15 at 17:20
  • 1
    The above transition is executed only if enabled in Developer options, see http://stackoverflow.com/a/30422015/2914140. – CoolMind Jul 25 '16 at 10:58
  • What value should I pass for fade_in if I want only the fade out effect without the fade in? – ATP Oct 08 '21 at 11:32
  • The built-in Android fade-in animation was very responsive without writing extra code. Thanks. – serif Dec 06 '22 at 18:17
27

Just re-posting answer by oleynikd because it's simple and neat

Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(getContext(),
    android.R.anim.fade_in, android.R.anim.fade_out).toBundle(); 
startActivity(intent, bundle);

To animate while pressing back button,

override fun onNavigateUp(): Boolean {
    return super.onNavigateUp()
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}

override fun onBackPressed() {
    super.onBackPressed()
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
Nikhil
  • 911
  • 15
  • 28
20

you can also use this code in your style.xml file so you don't need to write anything else in your activity.java

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- 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/AppTheme.WindowTransition</item>
</style>

<!-- Setting window animation -->
<style name="AppTheme.WindowTransition">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
Refnhaldy
  • 201
  • 2
  • 3
19

you can also add animation in your activity, in onCreate method like below becasue overridePendingTransition is not working with some mobile, or it depends on device settings...

View view = findViewById(android.R.id.content);
Animation mLoadAnimation = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
mLoadAnimation.setDuration(2000);
view.startAnimation(mLoadAnimation);
Enes
  • 2,225
  • 1
  • 19
  • 16
  • 1
    How to handle animation for the previous or parent activity? – Mehmed Sep 16 '14 at 21:04
  • 10
    To set the animation for the transition back to the parent activity use this code: `@Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); }` – John Verco Oct 03 '14 at 02:12