Activity transition in Android

AndroidAndroid ActivityTransition

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

An even easy way to do it is:

  1. Create an animation style into your styles.xml file

>

  1. Add this style to your app theme

>

That's it :)

Solution 4 - Android

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.

Solution 5 - Android

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.

Solution 6 - Android

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;

Solution 7 - Android

I overwrite my default activity animation. I test it in api 15 that it work smoothly. Here is the solution that I use:

<!-- 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/colorPrimary</item>
    <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>

</style>

<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
</style>

Create anim folder under res folder and then create this four animation files:

slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

You can download my sample project.

That's all... :)

Solution 8 - Android

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

Solution 9 - Android

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.

Solution 10 - Android

Before Starting your Intent:

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

This gives Default Animation to your Activity Transition.

Solution 11 - Android

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][1]

[1]: http://i.stack.imgur.com/HHH3i.jpg "tooltip"

Solution 12 - Android

Use ActivityCompat.startActivity() works API > 21.

    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionImage, EXTRA_IMAGE);
    ActivityCompat.startActivity(activity, intent, options.toBundle());

Solution 13 - Android

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

Solution 14 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhpiqueView Question on Stackoverflow
Solution 1 - AndroidBen ClaytonView Answer on Stackoverflow
Solution 2 - AndroidiandismeView Answer on Stackoverflow
Solution 3 - AndroidFelipe CondeView Answer on Stackoverflow
Solution 4 - AndroidCaseyBView Answer on Stackoverflow
Solution 5 - AndroidKevin C. KrinkeView Answer on Stackoverflow
Solution 6 - AndroidIceSteveView Answer on Stackoverflow
Solution 7 - AndroidShohan Ahmed SijanView Answer on Stackoverflow
Solution 8 - Androidsachin pangareView Answer on Stackoverflow
Solution 9 - AndroidCurtainView Answer on Stackoverflow
Solution 10 - AndroiddevDeejayView Answer on Stackoverflow
Solution 11 - AndroidMaher IsmaailView Answer on Stackoverflow
Solution 12 - AndroidMuhammad Aamir AliView Answer on Stackoverflow
Solution 13 - Androiduser8356857View Answer on Stackoverflow
Solution 14 - AndroidSergei EmelianovView Answer on Stackoverflow