1

Here is what I do:

a. The FragmentDialog has an layout inflated within onCreateView

` View layout = inflater.inflate(R.layout.my_layout, null);

   layMain = (LinearLayout) layout.findViewById(R.id.layMain);           
   final Animation anim = AnimationUtils.loadAnimation(getActivity(),  R.anim.translate_from_bottom);
   layMain.startAnimation(anim);`

b. The animation on creation works fine. However, I need to find a way, on dismiss (for example when the user presses the Back button) to run an animation and after that dismiss the FragmentDialog

c. I don't want to use android:windowEnterAnimation / android:windowExitAnimation as not all devices have animation active in Developer menu, and I need to run the animation on all cases.

So basically, what event should I override in order to run the animation, and on animation end to make the dismiss operation ?

Alin
  • 14,809
  • 40
  • 129
  • 218

1 Answers1

1

As found here https://stackoverflow.com/a/8209841/379865 the solution that works is pretty simple: get the dialog and listen for key presses.

getDialog().setOnKeyListener(new OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK)
                    {
                        final Animation anim =
                                AnimationUtils.loadAnimation(getActivity(),
                                        R.anim.translate_to_bottom);
                        anim.setAnimationListener(new AnimationListener() {

                            @Override
                            public void onAnimationStart(Animation animation) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                dismiss();

                            }
                        });
                        layMain.startAnimation(anim);
                        return true;
                    }
                    return false;
                }
            });
Community
  • 1
  • 1
Alin
  • 14,809
  • 40
  • 129
  • 218
  • what if the user presses outside the dialog? the animation doesn't work, the dialog just dismisses. I'm looking for a solution for that :/ – usernotnull Dec 23 '15 at 11:16