100

I want to swap two fragment in an activity via animation.Suppose PageA is for fragement A and left side on the screen and PageB is for fragment B i.e. on the right side of the screen. Now i want that when i click a button on pageA then PageA will move to the right side of the screen with some transition animation.

I tried the below code to replace the position

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new FragB());
fragmentTransaction.commit();

Looking for some clue.

starball
  • 20,030
  • 7
  • 43
  • 238
Deepak Goel
  • 5,624
  • 6
  • 39
  • 53
  • 1
    possible duplicate of [Animate the transition between fragments](http://stackoverflow.com/questions/4932462/animate-the-transition-between-fragments) – TalkLittle Jul 09 '14 at 20:57

1 Answers1

293

Old questiion and you probably already figured it out, but for future reference:

here's what you use to set a custom animation when you replace a fragment via code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.fragment_container, newFragment, "fragment");
// Start the animated transition.
ft.commit();

Here is an example of the slide_in_left animation:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="-100%"
   android:toXDelta="0"
   android:interpolator="@android:anim/decelerate_interpolator"
   android:duration="500"/>
</set>

Note that this is the animation if you are using the compatibility library. Instead if you are using and SDK with native support for the FragmentManager then your animation will look like this:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x" 
    android:valueType="floatType"
    android:valueFrom="-1280"
    android:valueTo="0" 
    android:duration="500"/>
</set>

This is because the compatibility library does not support the new objectAnimator type and instead only implement the old animation framework.

sciutand
  • 3,279
  • 1
  • 15
  • 5
  • 95
    This is definitely the right answer. Note that the order matters! You need to have the setCustomAnimations() call before the add/replace calls! – theelfismike Apr 10 '13 at 23:19
  • 2
    So which approach should u use if u want best comp? – basickarl Sep 24 '13 at 16:53
  • 17
    These different animations are not exactly equal. You are assuming the width of your fragment is equal to 1280 by using a valueFrom of -1280. If the width was greater it won't start completely off screen. – egfconnor Nov 05 '13 at 22:23
  • 58
    U can use the predefined animations from the android namespace instead: transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right); – Jordy Jan 27 '14 at 19:46
  • 2
    The predefined animations trigger a runtimeexception, FWIW, because they rely on the "translate" tag. –  Feb 04 '14 at 15:28
  • 1
    @egfconnor Have been looking for a solution to this, found a very long answer here: http://stackoverflow.com/questions/19372436/doing-a-push-fragment-animation is there any other way? – Zen May 28 '14 at 16:27
  • 1
    @alexsummers I recommend just using the support fragments instead of the actual fragments even on higher APIs. – egfconnor May 28 '14 at 17:20
  • @egfconnor But then I seem to get "Type mismatch: cannot convert from FragmentPreferences to Fragment" cuz im extending PreferenceFragment. PS: Just made the changes will update it once I have a solution – Zen May 28 '14 at 17:38
  • 1
    Is there an example of a slide_right_out in the objectAnimator framework you can offer? – loxdog Oct 21 '14 at 15:29
  • why doesn't the "support" library support `objectAnimator` ?? – Someone Somewhere Jan 10 '16 at 23:00