How to Dismiss the Dialog with Click on Outside of the Dialog

How to dismiss the dialog with click on outside of the dialog?

You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.

Something like,

  Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);

Or if your Dialog in non-model then,

1 - Set the flag-FLAG_NOT_TOUCH_MODAL for your dialog's window attribute

Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

2 - Add another flag to windows properties,, FLAG_WATCH_OUTSIDE_TOUCH - this one is for dialog to receive touch event outside its visible region.

3 - Override onTouchEvent() of dialog and check for action type. if the action type is
'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform.
view plainprint?

public boolean onTouchEvent(MotionEvent event)  
{

if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
System.out.println("TOuch outside the dialog ******************** ");
this.dismiss();
}
return false;
}

For more info look at How to dismiss a custom dialog based on touch points? and
How to dismiss your non-modal dialog, when touched outside dialog region

AlertDialog how to handle click outside of dialog view

You have to use this -

dialog.setCanceledOnTouchOutside(true);

Then for executing your function on outside click of dialog, do like this -

dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// dialog dismisses
// Do your function here
}
});

How to prevent AlertDialog box getting dismissed when clicked outside the dialog box?

Here is a simple dialog which cannot be canceled by clicking outside it:

class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// create dialog
val builder = AlertDialog.Builder(this).apply {

setPositiveButton("Ok",
DialogInterface.OnClickListener { dialog, id ->
// User clicked OK button
})

}

//This will make dialog unCancelable
val alertDialog: AlertDialog = builder.setCancelable(false).create()

// show dialog
alertDialog.show()
}
}

Prevent Android activity dialog from closing on outside touch

This could help you. It is a way to handle the touch outside event:

How to cancel an Dialog themed like Activity when touched outside the window?

By catching the event and doing nothing, I think you can prevent the closing. But what is strange though, is that the default behavior of your activity dialog should be not to close itself when you touch outside.

(PS: the code uses WindowManager.LayoutParams)

close html5 dialog by clicking outside of it

This caused me to look into the dialog element quite a bit. Just a forewarning, as I mentioned in my comments, the dialog element is lacking in support as its not supported by Safari or Firefox.

Here is a REPL you can run that contains the solution: https://replit.com/@heyitsmarcus/Dialog#index.html

But, I did find a solution here that works here on Stack Overflow: How to close the new html <dialog> tag by clicking on its ::backdrop

dialog.addEventListener('click', function (event) {
var rect = dialog.getBoundingClientRect();
var isInDialog=(rect.top <= event.clientY && event.clientY <= rect.top + rect.height
&& rect.left <= event.clientX && event.clientX <= rect.left + rect.width);
if (!isInDialog) {
dialog.close();
}
});

How to handle outside click on Dialog (Modal)?

I think what you need is disableBackdropClick passed down to <Modal /> component

<Modal disableBackdropClick />

You can also disable close Dialog on Esc key press with disableEscapeKeyDown prop

Created a custom layout dialog, but not able to dismiss dialog when clicked outside dialog window

    Dialog dialog = new Dialog(mContext);

I have tried removing the style from this dialog
and added the following :

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

Or

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

Both working fine and able to dismiss the dialog on the outside area click.



Related Topics



Leave a reply



Submit