DrkStr
DrkStr

Reputation: 1932

Trouble with animating fragment transaction animation

I am having some trouble trying to set custom animation to my fragment transaction. I know there are several treads on this and I have tried all their soluction and I still cant get this to work. Here are the animation xml files:

slide.down.xml(for exit)

<set xmlns:android="”http://schemas.android.com/apk/res/android”"
android:shareInterpolator="false" >

<translate
    android:duration="700"
    android:fromXDelta="0%"
    android:fromYDelta="-100%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

slide_up.xml(for entering)

<set xmlns:android="”http://schemas.android.com/apk/res/android”"
android:shareInterpolator="false" >

<translate
    android:duration="700"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="-100%"/>      

and my fragment transaction code:

getFragmentManager().beginTransaction()
                            .setCustomAnimations(R.anim.slide_up , R.anim.slide_down,R.anim.slide_up , R.anim.slide_down)
                            .replace(R.id.container, list)
                            .addToBackStack(null)
                            .commitAllowingStateLoss();

All the solutions form other similar threads I have tried:

I seem to be messing something very obvious and important but I cant figure out what that is.

My question: Why doesn't my costume animation work and how do I get it to work ?

Upvotes: 1

Views: 1599

Answers (1)

Paul Burke
Paul Burke

Reputation: 25584

So, there are two types of animations in Android. View Animation (Animation) and Property Animation (Animator). Your animations are the former, while FragmentTransaction.setCustomAnimations expects the latter. You have 2 options to fix this:

  1. Use the support library.
    • Subclass android.support.v4.app.Fragment
    • Use getSupportFragmentManager() to create the FragmentTransaction
  2. Use Animators
    • Create a custom parent ViewGroup (explained here)
    • Create the objectAnimator (XML)

The first option is definitely the easier one, especially since you're going for a on/off screen translation, and it expects the types of animations that you've already defined.

To learn more about the difference between the two animations, see here.

Upvotes: 4

Related Questions