12

How can I add animation to DialogFragment. My animations are:

out anim:

<scale
    android:duration="200"
    android:fillAfter="false"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="-90%"
    android:startOffset="200"
    android:toXScale="0.5"
    android:toYScale="0.5" />

<translate
    android:duration="300"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="-200"
    android:toYDelta="-200" />

in anim:

<scale
    android:duration="200"
    android:fillAfter="false"
    android:fromXScale="0.5"
    android:fromYScale="0.5"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="-90%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

<translate
    android:duration="300"
    android:fromXDelta="-200"
    android:fromYDelta="-200"
    android:toXDelta="0"
    android:toYDelta="0" />

and my code:

FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.jump_in, R.anim.jump_out, R.anim.jump_in, R.anim.jump_out);
                ft.add(layer_frag, "layer frag");
                ft.show(layer_frag).commit();//layer_frag is a class whitch extends DialogFragment

I must miss something because it appears as it appears before.

Csabi
  • 4,215
  • 15
  • 56
  • 104
  • Your answer is here: http://stackoverflow.com/a/13537234/969325. In short you have to hack your way around it as there is no build in way. – Warpzit Feb 25 '13 at 09:51
  • Probably not, because I did that to and it does not work for me as well :( it changed the layout a bit but the animation does not apear – Csabi Feb 25 '13 at 09:57
  • Then I think you are left with making your own Fragment which appears as a dialog. – Warpzit Feb 25 '13 at 09:59
  • Like this I have set the style and the custom theme, but the animation is left out :( – Csabi Feb 25 '13 at 10:03
  • // Pick a style based on the num. int style = DialogFragment.STYLE_NO_TITLE; setStyle(style, R.style.MyCustomTheme); – Csabi Feb 25 '13 at 10:03

1 Answers1

20
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().getAttributes().windowAnimations = R.style.detailDialogAnimation;
    return dialog;
}

The answer was from stackoverflow.com/a/13537234/969325 but You have to set the style at onCreateDialog function.

Community
  • 1
  • 1
Csabi
  • 4,215
  • 15
  • 56
  • 104
  • Do you know if there's a possible way to get a callback when these animations are completed? I'd like to do a 2-part animation, one where the dialog slides in, and then fading the views in. How would I attaach a listener to the windowAnimations? – android_student Apr 08 '15 at 22:11
  • setAnimationListener(new AnimationListener() { public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } public void onAnimationEnd(Animation animation) { ladder.startAnimation(toRight); } }); – Csabi Apr 09 '15 at 07:26
  • You don't have to set the style in `onCreateDialog`, as you can get the dialog using `getDialog` at any time after `onCreateDialog` has been called. I'm calling it in `onViewCreated` for example. – sotrh Nov 16 '16 at 19:37