Override Dialog dismiss() with new AlphaAnimation
Asked Answered
M

3

9

So right now by default the Dialog is doing this zoomin fade out effect when it gets dismissed with dialog.dismiss();

how can i override it to be my own Animation?

AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setDuration(600);
view.setAnimation(fadeOut);
view.startAnimation(fadeOut);

EDIT:

Thanks to the answer bellow i was able to figure it out. Instead of modifying the dismissal, i did the animation then dismissed it like so.

public void fadeOutHUD(View view) {
        AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setDuration(800);
        view.setAnimation(fadeOut);
        view.startAnimation(fadeOut);
        fadeOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

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

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

public void dismissHUD() {
        fadeOutHUD(findViewById(R.id.progressHud));
    }

And called it like so dialog.dismissHUD();

Mychal answered 22/9, 2013 at 4:44 Comment(1)
Where are you getting the view to fade out? I am trying to access the whole dialog as a view so I can fade it out like here, but I have only managed to get the dialog's content to fade (without the overlay background and white border).Russellrusset
R
7

I don't think you need to override the Dialog.dismiss()

You just animate the dialog as you wanted and at the end of animation, dismiss it.

@Override
public void onAnimationEnd(Animation animation) {
        dialog.dismiss();
}
Rate answered 22/9, 2013 at 5:12 Comment(2)
then how would i call the dismissing to tell it to call the animation?Mychal
Figured it out, you were so right, i'll update my question to provide a more detailed solution. Thanks againMychal
A
7

You will need to make use of

dialog.getWindow().getAttributes().windowAnimations flag to override the enter and exit/ dismiss animations.

This blog post explains to override the animation very well : http://flowovertop.blogspot.in/2013/03/android-alert-dialog-with-animation.html

Alesiaalessandra answered 22/9, 2013 at 5:1 Comment(0)
R
7

I don't think you need to override the Dialog.dismiss()

You just animate the dialog as you wanted and at the end of animation, dismiss it.

@Override
public void onAnimationEnd(Animation animation) {
        dialog.dismiss();
}
Rate answered 22/9, 2013 at 5:12 Comment(2)
then how would i call the dismissing to tell it to call the animation?Mychal
Figured it out, you were so right, i'll update my question to provide a more detailed solution. Thanks againMychal
W
0

Extend the Dialog class (it is recommended you use DialogFragment) and override its dismiss() using your own custom Animation.

Wellesley answered 22/9, 2013 at 4:59 Comment(1)
I did this and the Dialog just got stuck in a loop of showing, and would no go away, however it would flicker the animation like it was dismissing but then coming back immediately. Also i cant use a DialogFragment, when i do that it doesnt allow me to use findViewById()Mychal

© 2022 - 2024 — McMap. All rights reserved.