Reputation: 1117
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.
Upvotes: 21
Views: 18868
Reputation: 59004
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, use it in AndroidManifest.xml
<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.
Upvotes: 7
Reputation: 9287
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
Enjoy!!
Upvotes: 42
Reputation: 5144
Try below,
<?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" />
<?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" />
<?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);
Upvotes: 1
Reputation: 6697
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/
Upvotes: 3
Reputation: 13627
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
Upvotes: 15
Reputation: 1467
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.
Upvotes: 3