Activity animation slide from bottom Activity animation slide from bottom android android

Activity animation slide from bottom


You can achieve animations using below code :

bottom_up.xml :

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="1000"        android:fromYDelta="90%"        android:toYDelta="0" /></set>

bottom_down.xml :

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="1500"        android:fromYDelta="5"        android:toYDelta="90%" /></set>

nothing.xml :

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromYDelta="0%p"    android:toYDelta="0%p" />

Start Second Activity :

Intent intent = new Intent(MainActivity.this, SecondActivity.class);startActivity(intent);overridePendingTransition(R.anim.bottom_up, R.anim.nothing);

On finish of second activity :

@Overridepublic void onBackPressed() {    super.onBackPressed();    overridePendingTransition(R.anim.nothing, R.anim.bottom_down);}

Docs.:

Start an activity using an animation Activity transitions in material design apps provide visual connections between different states through motion and transformations between common elements. You can specify custom animations for enter and exit transitions and for transitions of shared elements between activities.

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.

Specify custom transitions First, enable window content transitions with the android:windowActivityTransitions attribute when you define a style that inherits from the material theme. You can also specify enter, exit, and shared element transitions in your style definition:

<style name="BaseAppTheme" parent="android:Theme.Material">   <!-- enable window content transitions -->   <item name="android:windowActivityTransitions">true</item>  <!-- specify enter and exit transitions -->   <item name="android:windowEnterTransition">@transition/explode</item>   <item name="android:windowExitTransition">@transition/explode</item> </style>

Please check doc. here

enter image description here

Enjoy!!


activity_slide_from_bottom.xml

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

activity_slide_to_bottom.xml

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

activity_stay.xml

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromYDelta="0%p"    android:toYDelta="0%p" />

Start NewActivity:

startActivity(NewActivity.getIntent(this));overridePendingTransition(R.anim.activity_slide_from_bottom, R.anim.activity_stay)

NewActivity finish():

@Overridepublic void onBackPressed() {    super.onBackPressed();    overridePendingTransition(R.anim.activity_stay, R.anim.activity_slide_to_bottom);}

Also, the animation can be declared in styles

<style name="Animation.MyCustomAnimation" parent="android:style/Animation.Activity">    <item name="android:activityOpenEnterAnimation">@anim/activity_slide_from_bottom</item>    <item name="android:activityOpenExitAnimation">@anim/activity_stay</item>    <item name="android:activityCloseEnterAnimation">@anim/activity_stay</item>    <item name="android:activityCloseExitAnimation">@anim/activity_slide_to_bottom</item></style>

Set this style in theme:

<style name="Theme.MyAnimTheme" parent="YourThemeParent">    <item name="android:windowAnimationStyle">@style/Animation.MyCustomAnimation</item></style>

Set the theme to your activity in manifest

<activity    android:name=".NewActivity"    android:theme="@style/Theme.MyCustomTheme" />

android:activityOpenEnterAnimation

When opening a new activity, this is the animation that is run on the next activity (which is entering the screen)

android:activityOpenExitAnimation

When opening a new activity, this is the animation that is run on the previous activity (which is exiting the screen).

android:activityCloseEnterAnimation

When closing the current activity, this is the animation that is run on the next activity (which is entering the screen).

android:activityCloseExitAnimation

When closing the current activity, this is the animation that is run on the current activity (which is exiting the screen).

android:windowReenterTransition

Reference to a Transition XML resource defining the desired Transition used to move Views in to the scene when returning from a previously-started Activity. Corresponds to Window.setReenterTransition(android.transition.Transition).

android:windowReturnTransition Reference to a Transition XML resource defining the desired Transition used to >move Views out of the scene when the Window is preparing to close. Corresponds to Window.setReturnTransition(android.transition.Transition).

Reference:https://developer.android.com/reference/android/R.attr


According to the documentation:

public void overridePendingTransition (int enterAnim, int exitAnim) Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.

As of Build.VERSION_CODES.JELLY_BEAN an alternative to using this with starting activities is to supply the desired animation information through an ActivityOptions bundle to startActivity(Intent, Bundle) or a related function. This allows you to specify a custom animation even when starting an activity from outside the context of the current top activity.

That implies Just adding overridePendingTransition(R.anim.activity_stay, R.anim.activity_slide_to_bottom) after startActivity(Intent, Bundle) Should give the needed result.

Edit:

android:activityOpenEnterAnimation android:activityOpenExitAnimation android:activityCloseEnterAnimation android:activityCloseExitAnimation

Effects the activity

android:windowEnterTransition android:windowExitAnimation android:windowReenterTransition android:windowReturnTransition

Effects the window

For more info about activity vs window:

An Activity has a window (in which it draws its user interface),

a Window has a single Surface and a single view hierarchy attached to it,

a Surface include ViewGroup which holds views.

Source: What is an Android window?

In conclusion, You can use them for achieving the same result but they are different.