6

I would like to set some animations to my custom dialog and I would like to do it programmatically. I know I can do it like this with xml animations:

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up_dialog</item>
    <item name="android:windowExitAnimation">@anim/slide_out_down</item>
</style>


Dialog imageDiaglog= new Dialog(MainActivity.this,R.style.DialogAnimation);

But I would like to do it programmatically. How can I set my programmatically created animations to Dialog's show() and hide() methods?

Thank you.

user4386126
  • 1,095
  • 3
  • 15
  • 32

2 Answers2

1

You cant because the dialog uses the style element for the animation transition. And You cannot set the style elements programmatically.

Hardik Chauhan
  • 2,652
  • 13
  • 28
0

You can use DialogFragment and set the animation in onCreateDialog(Dialog) or onStart(). Example from here:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().getAttributes().windowAnimations = R.style.detailDialogAnimation;
    return dialog;
}
Community
  • 1
  • 1
Jared Rummler
  • 35,743
  • 18
  • 127
  • 142
  • I need animation programatically.. not from animation file – Ranjith Kumar Nov 26 '15 at 09:17
  • You should be able to use `getDialog().getWindow().setEnterTransition(yourTransition);`. You may need to call `dialog.getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);` – Jared Rummler Nov 26 '15 at 10:18