Search code examples
androidanimationfadetransition

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?


Solution

  • 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.