How to add activity transitions programatically? [SOLVED] – Android Stduio


Starting from Android 5.0, you can add enter and exit animations for activities. These are also called Activity transitions.
An enter transition determines how views in an activity enter the scene. For example, in the explode enter transition, the views enter the scene from the outside and fly in towards the center of the screen.

An exit transition determines how views in an activity exit the scene. For example, in the explode exit transition, the views exit the scene away from the center.

Android 5.0 (API level 21) supports these enter and exit transitions:

  • explode – Moves views in or out from the center of the scene.
  • slide – Moves views in or out from one of the edges of the scene.
  • fade – Adds or removes a view from the scene by changing its opacity.

To enable window content transitions in your code, call the Window.requestFeature() method:

// inside your activity 
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);

// set an exit transition
getWindow().setExitTransition(new Explode());

****Just a quick note: Make sure that getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); is before the super.onCreate(savedInstanceState); otherwise it may crash or not work.

The setExitTransition() and setSharedElementExitTransition() methods define the exit transition for the calling activity. The setEnterTransition() and setSharedElementEnterTransition() methods define the enter transition for the called activity.

To get the full effect of a transition, you must enable window content transitions on both the calling and called activities. Otherwise, the calling activity will start the exit transition, but then you’ll see a window transition (like scale or fade).

To start an enter transition as soon as possible, use the Window.setAllowEnterTransitionOverlap() method on the called activity. This lets you have more dramatic enter transitions.

If you enable transitions and set an exit transition for an activity, the transition is activated when you launch another activity as follows:

startActivity(intent,
              ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

Now, you see, these features are only available Lollipop onwards so you would need to put the code related to animations inside an if statement, as shown below:
Add the following inside the OnCreate() Method of the ‘calling’ activity:

        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); //animation
        super.onCreate(savedInstanceState);
        //activity transition animation
        // inside your activity (if you did not enable transitions in your theme)

        // set an exit transition
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setExitTransition(new Explode());
            getWindow().setAllowEnterTransitionOverlap(true);
        }

Then the following in the OnCreate Method of the ‘called’ activity:

        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); //animation
        super.onCreate(savedInstanceState);

        //activity transition animation
        // inside your activity (if you did not enable transitions in your theme)

        // set an exit and enter transition
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setExitTransition(new Fade());
            getWindow().setEnterTransition(new Explode());
        }

And call the new activity as:

                        Intent intent = new Intent(MainActivity.this, New.class);
                        if (Build.VERSION.SDK_INT>=21){
                            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
                        }else{
                            startActivity(intent);
                        }
[wpedon id="7041" align="center"]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.