1

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:

  • Made sure setCustomAnimations is before replace
  • Have set android:hardwareAccelerated to true in the Manifest.
  • Made sure animations are not disabled in the developer options on my test device

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 ?

DrkStr
  • 1,752
  • 5
  • 38
  • 90
  • doesn't you have to use Animators, and not Animations? – pskink Sep 14 '14 at 19:42
  • I dont understand you, are you saying I should not use setCustomAnimations ? Why not ? – DrkStr Sep 14 '14 at 19:49
  • no, i am saying you should use objectAnimator in your xml file – pskink Sep 14 '14 at 20:18
  • Several people have implemented the way I have done it and it seems to work fine for them. http://stackoverflow.com/questions/11041325/android-custom-animation-on-fragment-transaction-not-running?rq=1 Can you link me to some examples where objectAnimator is used? – DrkStr Sep 14 '14 at 20:20
  • this is because they are using support library, and it uses Animations, 4+ code uses Animators, not Animations – pskink Sep 14 '14 at 20:28

1 Answers1

4

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.

Community
  • 1
  • 1
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • ad 2) you only need to have an Animator: either defined in xml or returned by Fragment.onCreateAnimator() – pskink Sep 15 '14 at 05:11
  • @pskink If you want to translate a view on/off screen, like he's doing above, you must create a custom parent with `Animator`. – Paul Burke Sep 15 '14 at 05:15
  • ok i overlooked "-100%", so in this case static xml file is not enough, but still you can override onCreateAnimatior without custom root ViewGrnoup – pskink Sep 15 '14 at 05:20
  • Hey @PaulBurke, and doing the fragment transactions in my FragmentActivity so I am importing android.support . v4 . app . FragmentActivity. I have replaced getFragmentManager() with getSupportFragmentManager(), but I am still not seeing the transaction animation. :( – DrkStr Sep 15 '14 at 05:55