How to prevent popup from closing by outside the window click in android?

十年热恋 提交于 2019-12-08 15:04:28

问题


In my activity i'm opening a popup (alert dialog) with yes and no option. But when I click outside the window or anywhere in the screen the popup is dismissed. How can I prevent the popup from closing by clicking anywhere. I want that it should close when I enter No button only. Please help!!!

Here is the popup code in MainActivity.java

Handler handler = new Handler();

handler.postDelayed(new Runnable() {

    @Override

    public void run() {


   if (context != null) {

            AlertDialog.Builder alert = new AlertDialog.Builder(context, R.style.MyAlertDialogStyle)
                    .setTitle("Title")
                    .setMessage("Message")
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // continue with delete
                            Intent intent = new Intent(MainActivity.this, WebActivity.class);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int which) {
                            dialogInterface.dismiss();
                            // do nothing
                        }
                    });

            mDialog = alert.create();
            mDialog.getWindow().getAttributes().windowAnimations = R.style.MyAlertDialogStyle;
            if (!((Activity) context).isFinishing())
            mDialog.show();
            // .setIcon(R.drawable.inr1)
            // .show();

        }
    }
}, 15000);

回答1:


Just add

mDialog.setCancelable(false);



回答2:


add this line before mDialog.show();

mDialog .setCanceledOnTouchOutside(false);



回答3:


Try mDialog.setCancelable(true);




回答4:


simply adding following line will solve your problem

mDialog .setCanceledOnTouchOutside(false);


来源:https://stackoverflow.com/questions/34265120/how-to-prevent-popup-from-closing-by-outside-the-window-click-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!