Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slide left/right animation between fragments

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 ?

like image 795
ludriv Avatar asked Dec 27 '13 14:12

ludriv


People also ask

What is Navigation animation?

Navigation and motion Navigation transitions use motion to guide users between two screens in your app. They help users orient themselves by expressing your app's hierarchy, using movement to indicate how elements are related to one another.

What are dynamic fragments?

Fragments are used when the user wants to see two different views of two different classes on the same screen. Fragments were added with Android Honeycomb. So if you are developing an application only for Android 3.0 (HoneyComb) or higher then Android provides you access to the Fragments class.


1 Answers

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 !

like image 139
ludriv Avatar answered Oct 08 '22 02:10

ludriv