17

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?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Piyush
  • 2,589
  • 6
  • 38
  • 77

4 Answers4

28

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 );
Community
  • 1
  • 1
Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
10

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

    }
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • 1
    Actually you have to switch the two lines. Call overridePendingTransition after super.onBackPressed() – Randalfien Nov 19 '12 at 10:48
  • I wonder why they should first `super` be called and `override` only after? From the first glance it seems more logical to first "override" transition and then actual trigger the change, isn't it? – Dimitry K Mar 21 '14 at 18:50
  • override fun onPause() { super.onPause() if (isFinishing) overridePendingTransition(..., ...) } – Stan Oct 13 '19 at 17:46
0

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.

Shivam Yadav
  • 958
  • 11
  • 23
-1

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.

chartsai
  • 368
  • 3
  • 7