ludriv ludriv - 5 months ago 107
Android Question

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)

FragmentTransaction ft = getFragmentManager().beginTransaction();

ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

ft.replace(, fragment, fragment.getClass().getSimpleName()).commit();



<?xml version="1.0" encoding="utf-8"?> <set xmlns:android=""

android:toXDelta="0" />

android:toAlpha="1.0" />


And finally R.anim.slide_out_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=""
android:duration="@android:integer/config_mediumAnimTime" >

android:toXDelta="-100%p" />

android:toAlpha="0.0" />


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 ?


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 !