Activity animation slide from bottom
Asked Answered
D

6

21

What I use:

activity_stay.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="@android:integer/config_longAnimTime"
        android:fromYDelta="0%p"
        android:toYDelta="0%p" />

</set>

activity_slide_to_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">

    <translate
        android:duration="@android:integer/config_longAnimTime"
        android:fromYDelta="0"
        android:toYDelta="100%p" />

</set>

activity_slide_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">

    <translate
        android:duration="@android:integer/config_longAnimTime"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

</set>

Start NewActivity:

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

NewActivity finish():

finish()
overridePendingTransition(R.anim.activity_stay, R.anim.activity_slide_to_bottom)

When NewActivity is started OldActivity disappear - I see blank white screen above which NewActivity slide to top. However what I need is my NewActivity sliding to top above the OldActivity content when started. How can I achieve this?

UPD: when I finish my NewActivity for some reasons all animations execute perfectly: NewActivity slide to bottom with OldActivity content appearing below NewActivity content.

Dis answered 30/3, 2018 at 8:40 Comment(6)
try using same duration for stay @android:integer/config_longAnimTimeTheoretics
@JyotiJK Thanks for answer, i tried it but it didn't help.Dis
this link show why you should choose the right cycle for transition animation https://mcmap.net/q/408131/-make-activity-animate-from-top-to-bottomSharika
If you want the transition work for whole application you can create a rootacivity and inherit it in the activity you need. see example https://mcmap.net/q/101676/-android-left-to-right-slide-animationSharika
If you want to go with your method only try to overide you finish method and add a condition of is finishing to check and pass your overidependingtranstion() method there i hardly think that might work but you can trySharika
By these three animations what are you trying to do? What exactly you want? Just need animation on Intent call ?Mendelson
F
42

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 :

@Override
public 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!!

Fecit answered 7/7, 2018 at 7:29 Comment(2)
What is the difference between android:activityOpenEnterAnimation android:activityOpenExitAnimation android:activityCloseEnterAnimation android:activityCloseExitAnimation and android:windowEnterTransition android:windowExitAnimation android:windowReenterTransition android:windowReturnTransition When should you use one instead of the other?Kubiak
you helped me a lot man :) ❤Viyella
C
15

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

@Override
public 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

Cubbyhole answered 7/7, 2018 at 8:2 Comment(1)
What is the difference between android:activityOpenEnterAnimation android:activityOpenExitAnimation android:activityCloseEnterAnimation android:activityCloseExitAnimation and android:windowEnterTransition android:windowExitAnimation android:windowReenterTransition android:windowReturnTransition When should you use one instead of the other?Kubiak
B
5

Best way is to use styles, because you don't have to code in every Activity in case if you want this animation in all your activities.

Shortest way that i use always

To set slide theme in all your activities

<application
    android:theme="@style/Theme.SlideAnimWindow"
    ...
    >

To set slide theme in one activity

<activity
    android:name=".YourActivity"
    android:theme="@style/Theme.SlideAnimWindow" />

Put slide_from_bottom.xml in your res>anim.

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

Put slide_to_bottom.xml in your res>anim.

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

Put none.xml in your res>anim.

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

Put this style in res>values>styles.xml

<style name="SlideAnimation" parent="android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_from_bottom</item>
    <item name="android:activityOpenExitAnimation">@anim/none</item>
    <item name="android:activityCloseEnterAnimation">@anim/none</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_to_bottom</item>
</style>

<style name="Theme.SlideAnimWindow" parent="AppTheme">
    <item name="android:windowAnimationStyle">@style/SlideAnimation</item>
</style>

You can change android:duration="@android:integer/config_longAnimTime" duration according to your need. Now you are good to go.

Burp answered 13/7, 2018 at 10:23 Comment(0)
S
3

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.

Sigman answered 7/7, 2018 at 8:3 Comment(1)
What is the difference between android:activityOpenEnterAnimation android:activityOpenExitAnimation android:activityCloseEnterAnimation android:activityCloseExitAnimation and android:windowEnterTransition android:windowExitAnimation android:windowReenterTransition android:windowReturnTransition When should you use one instead of the other?Kubiak
K
3

I think the Animations API is more preferrable to the Transitions API for what you are trying to accomplish. Here is a detailed explanation : https://www.reddit.com/r/androiddev/comments/8wqjzv/difference_between_animation_and_transitions_api/

Kubiak answered 7/7, 2018 at 17:53 Comment(0)
C
1

Try below,

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

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

no_anim.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.slide_in_up, R.anim.no_anim);

NewActivity finish():

finish();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_down);
Capstone answered 13/7, 2018 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.