I want an animation while switching from one activity to another in Android. The animation I'm aiming for is a bottom to top like animation.
How can I do that?
I want an animation while switching from one activity to another in Android. The animation I'm aiming for is a bottom to top like animation.
How can I do that?
Yes it is possible. check out this question. You have to define animations in anim folder than you can overide current animation using
overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
You can set your animation when you go to another activity using this
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
Also you can get same animation if you come back from last activity to previous activity by overriding method
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
A better way to do this is to create a style like below
<style name="SlideAnimation.Activity" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/slide_from_top</item>
<item name="android:activityOpenExitAnimation">@anim/slide_to_bottom</item>
<item name="android:activityCloseEnterAnimation">@anim/slide_from_bottom</item>
<item name="android:activityCloseExitAnimation">@anim/slide_to_top</item>
</style>
If you want to implement this for whole application then use it in app theme like be
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorWhite</item>
<item name="colorPrimaryDark">@color/colorWhite</item>
<item name="colorAccent">@color/colorAppBlue</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowAnimationStyle">@style/SlideAnimation.Activity</item>
</style>
And declare the AppTheme in manifest in application tag like below-
<application
android:name=".MyApp"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme"
>
And if you want to use for specific activity then apply the theme to that activity in manifest.
You can override the public boolean onOptionsItemSelected(MenuItem item)
function, and use finish()
followed by overridePendingTransition()
.
For example, add the following code in your activity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // navigation up.
finish();
overridePendingTransition(R.anim.ENTER_ANIMATION, R.anim.EXIT_ANIMATION);
return true;
case ....: // implementation of other menu items.
}
return super.onOptionsItemSelected(item);
}
The other way is overwriting the public boolean onNavigateUp()
function. But onNavigateUp()
is only for API level 16 and above.