Search code examples
androidxmlanimationnavigationandroid-architecture-navigation

Reverse enter and exit animation for RTL layout - Android Navigation Component


I'm using the Android Navigation Architecture Component for navigation between fragments. I've set enter and exit animation for one of the fragment destinations. Animation works correctly and the same for both Left-to-Right and Right-to-Left layouts. I need this animation should consider opposite edges for RTL layout, i.e. what start and end properties do instead of left and right. What is the simplest way to achieve the same?

<fragment
    android:id="@+id/sign_in_fragment"
    android:name="com.example.SignInFragment">

    <action
        android:id="@+id/action_sign_up"
        app:destination="@id/sign_up_fragment"
        app:enterAnim="@anim/slide_in_right"
        app:exitAnim="@anim/slide_out_left"/>

</fragment>

<fragment
    android:id="@+id/sign_up_fragment"
    android:name="com.example.SignUpFragment"/>

anim/slide_out_left

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
</set>

anim/slide_in_right

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
</set>

Solution

  • The same like you do for different density versions of drawables, you can have RTL version of your anim resources in anim-ldrtl with the same anim resource names but exchange the behavior.

    For instance slide_out_left.xml in res\anim-ldrtl\ will contain the anim of slide_out_right.xml and so on.

    You can create anim-ldrtl directory under res or you can use Android studio for that:

    Right-Click on res\anim -> New -> Animation Resource File

    enter image description here

    enter image description here