8

I'm trying to use the Jetpack Navigation component. The docs here talk about animating transitions. The example code uses the animations slide_in_right and slide_out_left and acts like they will be there by default - there is no instruction on how to create them.

<action
    ...
    app:enterAnim="@anim/slide_in_right"
    app:exitAnim="@anim/slide_out_left"
    app:popEnterAnim="@anim/slide_in_left"
    app:popExitAnim="@anim/slide_out_right" />

But when I click on the attributes as shown below, in the design view of the navigation graph resource, I only see slide_in_left and side_out_right. Why are the other two not there?

My goal is to make a push/pop like animation where the new view comes in from the right and the old view moves out to the left. (Reverse for "popping" back in the nav stack.)

I do see some other questions about these animations, but they answers are old and it sounds like there may have been a bug, so I'm wondering what the answer is now in 2020.

enter image description here

enter image description here

Rob N
  • 15,024
  • 17
  • 92
  • 165

2 Answers2

17

slide_in_rigth and slide_out_left animation can be found in SDK but when I tried to use from XML I got this error:

AAPT: error: resource android:anim/slide_in_right is private.

So I copied content of animations to my resource files as below:

enter image description here

Here are contents of animations which I copied from Android SDK. For slide_in_right.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
    </set>

For slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

Now they can be use with @animator so I added to my navigation.xml as below:

    <action
        ...
        app:enterAnim="@animator/slide_in_right"
        app:exitAnim="@animator/slide_out_left" />

It works for me, I hope it will help you.

kiroglue
  • 341
  • 2
  • 7
  • I had to extract both the integer config constants as well as the animation for being private, but got it to work, but the bigger question is why? What it Google's motivation for keeping these private and unusable? And also, where are these marked private in the SDK? I see the definitions in Git, but can't find where they are marked private. – penguin359 Aug 11 '22 at 03:08
  • Same issue here, they don't appear any different than the other left right animations, how and where are they marked private and why :( – phazei Mar 29 '23 at 18:26
  • This has been an issue for over a decade... https://stackoverflow.com/questions/1274657/android-how-to-get-android-r-anim-slide-in-right – phazei Mar 29 '23 at 18:29
0

The other answer explains how you can use them by copying them locally, but not actually why it's like that.

I looked into it and did a search. The animation files are located here:

\AppData\Local\Android\Sdk\platforms\android-33\data\res\anim

But, there's actually a file that specifies files that are public, and if they are not in that file then they are private by default. That is this file:

\AppData\Local\Android\Sdk\platforms\android-33\data\res\values\public-final.xml

or previously:

\AppData\Local\Android\Sdk\platforms\android-31\data\res\values\public.xml

Unfortunately, every entry in that has an incrementing resource id, and it could break compatibility if they're modified. And if you add something at the end, it would probably mess things up when the next version comes out with new resources and expect those id's to be something else. It's kind of ridiculous that there are actually so many animations in \res\anim that simply aren't listed in the app because they weren't added to the public.xml and it's been like that for over a decade...

Here's a great explanation of the public.xml https://stackoverflow.com/a/9357813/65985

These are some related questions:

Android - how to get android.R.anim.slide_in_right

Android how to access slide_in_right.xml animation

missing animation in R.anim and Progressbar in ImageSwitcher

phazei
  • 5,323
  • 5
  • 42
  • 46