6

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 ?

Community
  • 1
  • 1
ludriv
  • 291
  • 1
  • 6
  • 15
  • possible duplicate of [Android - Custom Animation on fragment transaction not running](http://stackoverflow.com/questions/11041325/android-custom-animation-on-fragment-transaction-not-running) – Devrim Dec 27 '13 at 14:31
  • Have you check this ? http://stackoverflow.com/questions/4932462/animate-the-transition-between-fragments – Piyush Dec 27 '13 at 14:34
  • aegean : it's look like, but not a duplicate @PiyushGupta : yes I already read, I tried too and I got another error : Method setXFraction() with type float not found on target class class android.widget.LinearLayout – ludriv Dec 27 '13 at 14:38
  • For that you have to use FrameLayout instead of LinearLayout – Piyush Dec 27 '13 at 14:40
  • So I need to have a FrameLayout subclass per view I need to animate ? – ludriv Dec 27 '13 at 14:41
  • Thank you @PiyushGupta, I thought there was a less boring way! – ludriv Dec 27 '13 at 14:57

1 Answers1

2

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 !

Jeremy Wiebe
  • 3,894
  • 22
  • 31
ludriv
  • 291
  • 1
  • 6
  • 15