Activity transition in Android
Asked Answered
S

14

198

How can I define the transition between two activities for Android 1.5 and later? I would like an activity to fade in.

Seaquake answered 2/8, 2010 at 16:3 Comment(1)
Applies to all overridePendingTransition-related answers below: You can pass (0, 0) if you want no animation at all.Sawfish
Z
168

You can do this with Activity.overridePendingTransition(). You can define simple transition animations in an XML resource file.

Zonked answered 2/8, 2010 at 16:9 Comment(9)
Thanks iandisme. overridePengingTransition is API level 5. Is it not possible to do this for level 3 (Android 1.5)?Seaquake
Ah, you're right. CaseyB's answer is probably more along the lines of what you're looking for.Zonked
Haven't found yet how to do a proper fade in with CaseyB's answer.Seaquake
You can do this in your Activity's onCreate function.Pennywise
On HTC you have to change settings > display > animation to all for it to work (or at least on HTC Desire HD).Moonset
is it possible on canvas ? it yes, can you plz help me.Shroyer
Are there any built into Android or are we left to our own devices?Eadmund
Here's the century of upvotes - this was the missing piece of my transition jigsaw :)Malleolus
The link to the tutorial is brokenDesman
A
203

Here's the code to do a nice smooth fade between two Activities..

Create a file called fadein.xml in res/anim

<?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="2000" />

Create a file called fadeout.xml in res/anim

<?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:duration="2000" />

If you want to fade from Activity A to Activity B, put the following in the onCreate() method for Activity B. Before setContentView() works for me.

overridePendingTransition(R.anim.fadein, R.anim.fadeout);

If the fades are too slow for you, change android:duration in the xml files above to something smaller.

Ailanthus answered 28/2, 2011 at 17:17 Comment(9)
Just to add to this. overridePendingTransition() will need to be called again, right after the OS decides to close your Activity. I just put another identical call to overridePendingTransition(fadein,fadeout) in the Activity's onPause() method. Otherwise, you'll see the Activity fade in, but not fade out when closed.Papert
This doesn't work. I also tried the additional step in the comment from Nate and it behaves the same. I posted a new thread here: #11723982 This describes what is occurring and hopefully will have a fix! :-)Hanhana
No answers on other thread. I'm deleting it. What I'm experiencing is what happens is the transition occurs immediately and then it goes dark and fades in. So I'm on Activity A and Activity B is displayed then goes dim and fades in. I then modified it to follow the instructions with adding the code to the onPause() of Activity A and get the same behavior.Hanhana
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).Jutland
Thanks, got this working. But how to do the same when user presses back button?Filmdom
Just don't forgot to change fadein and fadeout to fade_in and fade_out. From Dan J's postCushing
When using: overridePendingTransition(android.R.anim.fadein, android.R.anim.fadeout); Is it possible to make the transitions longer in an easy way?Wrand
According to the docs, you should call overridePendingTransition() right after calling finish() and/or startActivity(). I was able to get a nice fade this way by calling it right after launching the new Activity.Seidel
Activities animations are so ugly. GeezzzzVitriform
Z
168

You can do this with Activity.overridePendingTransition(). You can define simple transition animations in an XML resource file.

Zonked answered 2/8, 2010 at 16:9 Comment(9)
Thanks iandisme. overridePengingTransition is API level 5. Is it not possible to do this for level 3 (Android 1.5)?Seaquake
Ah, you're right. CaseyB's answer is probably more along the lines of what you're looking for.Zonked
Haven't found yet how to do a proper fade in with CaseyB's answer.Seaquake
You can do this in your Activity's onCreate function.Pennywise
On HTC you have to change settings > display > animation to all for it to work (or at least on HTC Desire HD).Moonset
is it possible on canvas ? it yes, can you plz help me.Shroyer
Are there any built into Android or are we left to our own devices?Eadmund
Here's the century of upvotes - this was the missing piece of my transition jigsaw :)Malleolus
The link to the tutorial is brokenDesman
A
52

An even easy way to do it is:

  1. Create an animation style into your styles.xml file
<style name="WindowAnimationTransition">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
  1. Add this style to your app theme
<style name="AppBaseTheme" parent="Theme.Material.Light.DarkActionBar">
      <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
</style>

That's it :)

Altricial answered 28/11, 2014 at 1:25 Comment(1)
I can't even launch the app ! Caused by: java.lang.RuntimeException: Unknown scene name: alphaPiton
M
32

Yes. You can tell the OS what kind of transition you want to have for your activity.

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getWindow().setWindowAnimations(ANIMATION);

    ...

}

Where ANIMATION is an integer referring to a built in animation in the OS.

Microfilm answered 2/8, 2010 at 16:10 Comment(6)
Do I have to do something else for it to work? getWindow().setWindowAnimations(android.R.anim.fade_in) doesn't result in the push transition that was used by default, but it's not a fade transition either. The new activity just appears over the previous one in a Nexus One device.Seaquake
That's because this isn't asking for a resource, it's asking for the id of a transition animation built into the OS. developer.android.com/intl/fr/reference/android/view/…Microfilm
Can you provide an example? I can't find the constant. Thanks.Seaquake
It seems setWindowAnimations only accepts style resources. getWindow().setWindowAnimations(android.R.style.Animation_Toast) is the closest I've found to a fade in, but it fades from black, not the previous activity.Seaquake
It doesn't have to be built in animation in the OS, you can definite a custom one in values.Halpin
@ilija139, are you sure? It says here developer.android.com/reference/android/view/…: "This must be a system resource; it can not be an application resource because the window manager does not have access to applications."Coptic
K
30

For a list of default animations see: http://developer.android.com/reference/android/R.anim.html

There is in fact fade_in and fade_out for API level 1 and up.

Kalidasa answered 24/1, 2012 at 7:37 Comment(2)
e.g. getWindow().setWindowAnimations(android.R.anim.slide_in_left);Kinzer
Cheers, took me a surprisingly long time Googling to find these.Tenement
D
29

create res>anim>fadein.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" />

create res>anim>fadeout.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:duration="500" />

In res>values>styles.xml

<style name="Fade">
        <item name="android:windowEnterAnimation">@anim/fadein</item>
        <item name="android:windowExitAnimation">@anim/fadeout</item>
    </style>

In activities onCreate()

getWindow().getAttributes().windowAnimations = R.style.Fade;
Domineering answered 14/5, 2012 at 1:9 Comment(3)
I would suggest adding it to a theme so it is applied to all activities?Anticathode
These animations are already defined in Android, simply add the following code in onPause() of all activities: overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);Triple
Yes, just wanted to show that the animation can be edited to the developers liking.Domineering
B
26

Here's the code to do a nice smooth between two activity.

  1. smooth effect from left to right

    Create a file called slide_in_right.xml and slide_out_right.xml in res/anim

    slide_in_right.xml

        <?xml version="1.0" encoding="utf-8"?>
        <set xmlns:android="http://schemas.android.com/apk/res/android"
            android:shareInterpolator="false" >
            <translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" />
            <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
        </set>
    

    slide_out_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false" >
        <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/>
        <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
    </set>
    
  2. smooth effect from right to left

    Create a file called animation_enter.xml and animation_leave.xml in res/anim

    animation_enter.xml

       <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate android:fromXDelta="-100%" android:toXDelta="0%"
            android:fromYDelta="0%" android:toYDelta="0%"
            android:duration="700"/>
       </set>
    

    animation_leave.xml

      <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate
            android:fromXDelta="0%" android:toXDelta="100%"
            android:fromYDelta="0%" android:toYDelta="0%"
            android:duration="700" />
      </set>
    
  3. Navigate from one activity to second Activity

       Intent intent_next=new Intent(One_Activity.this,Second_Activity.class);
       overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
       startActivity(intent_next);
     finish();
    

    4.On back press event or Navigate from second activity to one Activity

     Intent home_intent = new Intent(Second_Activity.this, One_Activity.class);
     overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
     startActivity(home_intent);
     finish();
    
Boren answered 31/7, 2015 at 7:21 Comment(4)
The overridePendingTransition should be after startActivity in 3.Aziza
The overridePendingTransition should be after startActivity in 3 and 4Dalpe
It is not working for me if overridePendingTransition() put before startActivity()Newfeld
Should call overridePendingTransition immediately after startActivity: developer.android.com/reference/android/app/…Tetzel
O
4

You cannot use overridePendingTransition in Android 1.5. overridePendingTransistion came to Android 2.0.

If you're gonna go through this without any error you have to compile for the target (1.5 or higher) using the ordinary animations (or you own) or you have to compile for the target (2.0 or higher) using overridePendingTransistion.

Summary: You cannot use overridePendingTransistion in Android 1.5.

You can though use the built-in animations in the OS.

Outvote answered 2/8, 2010 at 16:50 Comment(2)
That is not correct. Animations are in Android well before 1.6 and you can use overridePendingTransistion with reflection to still target 1.5.Seaquake
Well, my mistake. Updated my post. You can surely do your own animations and customizing them as you want in 1.5. But you can still not use overridePendingTransition since it started to appear in API-level 5.Outvote
L
3

Before Starting your Intent:

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(AlbumListActivity.this);
startActivity(intent, options.toBundle());

This gives Default Animation to your Activity Transition.

Legal answered 6/9, 2017 at 15:39 Comment(0)
E
2

IN GALAXY Devices :

You need to make sure that you havn't turned it off in the device using the Settings > Developer Options:

two muppets

Escamilla answered 24/5, 2015 at 9:35 Comment(3)
even for LG devices :)Hankhanke
I think so ,, but i dont try itEscamilla
Fantastic! I found your answer after several hours of searching transitions and animations.Kalman
B
1

Use ActivityCompat.startActivity() works API > 21.

    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionImage, EXTRA_IMAGE);
    ActivityCompat.startActivity(activity, intent, options.toBundle());
Baboon answered 25/11, 2015 at 18:46 Comment(0)
P
1

zoom in out animation

Intent i = new Intent(getApplicationContext(), LoginActivity.class);
 overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
startActivity(i);
finish();

zoom_enter

<?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" />

zoom_exit

<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" />
Parlance answered 30/5, 2018 at 7:14 Comment(1)
"overridePendingTransition" will be called after startActivity() methodDerte
B
0

Some versions of Android support custom Activity transitions and some don't (older devices). If you want to use custom transitions it's good practice to check whether the Activity has the overridePendingTransition() method, as on older versions it does not.

To know whether the method exists or not, reflection API can be used. Here is the simple code which will check and return the method if it exists:

Method mOverridePendingTransition;

try {
        mOverridePendingTransition = Activity.class.getMethod(
                "overridePendingTransition", new Class[] { Integer.TYPE, Integer.TYPE } );
        /* success */
    } catch (NoSuchMethodException nsme) {
        /* failure, this version of Android doesn't have this method */
    } 

And then, we can apply our own transition, i.e. use this method if it exists:

if (UIConstants.mOverridePendingTransition != null) {
               try {
                   UIConstants.mOverridePendingTransition.invoke(MainActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
               } catch (InvocationTargetException e) {
                   e.printStackTrace();
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               }
            }

Here, as an example, simple fade-in and fade-out animations were used for transition demonstration..

Bertha answered 20/5, 2018 at 11:55 Comment(0)
K
0

Kotlin Extension Function

fun Context.transition(enterTransition: Int, exitTransition: Int) {
    (this as Activity).overridePendingTransition(enterTransition, exitTransition)
}
Kentonkentucky answered 28/10, 2023 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.