0

I am trying to add animation to alert dialog when it is dismiss when user touch outside of it. by default when it is touched outside, the dialog just fades out or disappears. Is there a way to override the default dimiss or cancel? I was able to add animation when a button is pressed and when it call but cannot figure out how to add the animation to the default close action when it is touched outside. please help. Thanks in advance

I tried but it did not work

alert.setOnCancelListener()
alert.setOnDismissListener()

here is my code:

View rl = getActivity().findViewById(R.id.map) ;

                AlertDialog.Builder alert = new 
AlertDialog.Builder(rl.getContext(), R.style.CFDialog);
                LayoutInflater inflater = 
getActivity().getLayoutInflater();
                View v = 
inflater.inflate(R.layout.dialog_footer_layout, null);

                Animation transition_in_view = 
AnimationUtils.loadAnimation(rl.getContext(), 
R.anim.alert_present);
                Animation transition_out_view = 
AnimationUtils.loadAnimation(rl.getContext(), 
R.anim.alert_dismiss);

                //customer animation appearance

                v.setAnimation( transition_in_view );
                v.startAnimation( transition_in_view );

                alert.setView(v);
                alert.setOnCancelListener(new 
DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface 
dialogInterface) {

                        v.startAnimation(transition_out_view);

                    }
                });

                alert.setOnDismissListener()
                Button button = 
v.findViewById(R.id.configuration_toggle_button);

                AlertDialog helpDialog = alert.create();
                button.setOnClickListener(new 
View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        v.startAnimation(transition_out_view);

transition_out_view.setAnimationListener(new 
Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation 
animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation 
animation) {

                                helpDialog.dismiss();

                            }

                            @Override
                            public void onAnimationRepeat(Animation 
animation) {

                            }
                        });

                    }
                });

                helpDialog.setOnCancelListener(new 
DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface 
dialogInterface) {
                        v.startAnimation(transition_out_view);
                        return;
                    }
                });

                helpDialog.setOnDismissListener(new 
DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface 
dialogInterface) {
                        v.startAnimation(transition_out_view);
                        return;
                    }
                });

                // Hide after some seconds
                final Handler handler  = new Handler();
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        if (helpDialog.isShowing()) {

                            v.startAnimation(transition_out_view);

transition_out_view.setAnimationListener(new 
Animation.AnimationListener() {
                                @Override
                                public void 
onAnimationStart(Animation animation) {

                                }

                                @Override
                                public void 
onAnimationEnd(Animation animation) {

                                    helpDialog.dismiss();

                                }

                                @Override
                                public void 
onAnimationRepeat(Animation animation) {

                                }
                            });
                        }
                    }
                };

                alert.setOnDismissListener(new 
DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        handler.removeCallbacks(runnable);
                    }
                });


                handler.postDelayed(runnable, 5000);



helpDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WindowManager.LayoutParams wmlp = 
helpDialog.getWindow().getAttributes();


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                {
                    wmlp.gravity = Gravity.TOP;

                    wmlp.y = 200;   //y position
                    helpDialog.show();
                } else {

//                        if (Build.VERSION.SDK_INT <= 
Build.VERSION_CODES.O)

                    wmlp.gravity = Gravity.TOP;

                    wmlp.y = 180;   //y position
                    helpDialog.show();

                }

Here is the style to the dialog:

<style name="CFDialog" parent="Theme.AppCompat.Translucent">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimAmount">0.5</item>
    <item name="colorPrimaryDark">@color/transparent</item>
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
NewCEO
  • 51
  • 1
  • 7

1 Answers1

0

What I wanted to do was to add an animation to the dialog when it is touched outside. The touch outside code was easy but a way to animate was hard. I want it to slide up.

After a week of trying to find a solution, I have found a way to override the default dialog.setCanceledOnTouchOutside(true) and set an animation to it.

Here is what I did. I research the http://iserveandroid.blogspot.com/2011/04/how-to-dismiss-your-non-modal-dialog.html and from here I was able to search for how to add animation to FLAG_WATCH_OUTSIDE_TOUCH and came across this WindowManager with Animation (is it possible?)

From here I was able to implement this wmlp.windowAnimations = R.style.CFDialog_Animation;

Here is my code:

AlertDialog helpDialog = alert.create();

helpDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WindowManager.LayoutParams wmlp = 
helpDialog.getWindow().getAttributes();


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                {
                    wmlp.gravity = Gravity.TOP;

                    wmlp.y = 200;   //y position
                    helpDialog.show();
                } else {

//                        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O)

                    wmlp.gravity = Gravity.TOP;
                    wmlp.y = 180;   //y position
                    helpDialog.show();

                }

                //this code below is what overrides it and add the animation

                wmlp.windowAnimations = R.style.CFDialog_Animation; 




        ///optional, your preference

 Window window = helpDialog.getWindow();

window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

window.setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

I also utilize the optional codes to make it non model so it can be canceled/dismiss when you touch on an object outside the dialog (optional)

Now when you click outside the dialog, it slides up. Of course you can use your own animation. wmlp.windowAnimations = android.R.style.Animation_Translucent; makes it go sideway. I don't know why.

Here is my style:

<style name="CFDialog.Animation">

    <item name="android:windowExitAnimation">@anim/alert_dismiss</item>
</style>

and alert_dimiss animation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<alpha
    android:interpolator="@android:anim/anticipate_interpolator"
    android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />

<translate android:fromYDelta="00%" android:toYDelta="-100%" 
android:duration="500"/>
</set>

Hope this help others looking for a solution

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
NewCEO
  • 51
  • 1
  • 7