Slide left/right animation between fragments

32,551

Problem solved!

Like Piyush Gupta said, I must have a custom subclass of FrameLayout per Fragment I need to animate, first.

Secondly, I must not use R.anim but R.animator like another similar post (link in question).

Thank you all !

Share:
32,551
ludriv
Author by

ludriv

Updated on July 09, 2022

Comments

  • ludriv
    ludriv almost 2 years

    I post a question here because I can't find out a solution to my problem. I read a lot of things about android animation.

    I actually develop an android 4.0 app and I need to animate transition between fragments (not layout).

    Similary post, worked with layout but no more precision about fragments

    Here my uncomplete code :

    Activity code

    private void showFragment(final Fragment fragment)
    {
        if (null == fragment)
            return;
    
        FragmentTransaction ft = getFragmentManager().beginTransaction();
    
        ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
    
        ft.replace(R.id.fragment_container_layout, fragment, fragment.getClass().getSimpleName()).commit();
    
    }
    

    R.anim.slide_in_left

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

    And finally R.anim.slide_out_right

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

    When I run this code, I got an exception : 12-27 15:26:55.566: E/AndroidRuntime(27699): java.lang.RuntimeException: Unknown animator name: translate

    Do you have any idea to fix this ?