Prevent Android Activity Dialog from Closing on Outside Touch

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)

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 dialog from closing on outside touch in Flutter

There's a property called barrierDismissible that you can pass to showDialog ; which makes dialogs dismissible or not on external click

showDialog(
barrierDismissible: false,
builder: ...
)

How to prevent a DialogFragment from dismissing on touch outside?

Do you want to achieve this result? If you click the outside the Dialog, we can detect the click event before the Dialog closed.
Sample Image

You can create a MyAlertDialog inherit the AlertDialog

    public class MyAlertDialog:AlertDialog
{
public MyAlertDialog(Context c):base(c) {

}

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//Please add following code for watching outside touch
this.Window.SetFlags(WindowManagerFlags.NotTouchModal, WindowManagerFlags.NotTouchModal);
this.Window.AddFlags(WindowManagerFlags.WatchOutsideTouch);
}

public override bool OnTouchEvent(MotionEvent e)
{
//just used for testing.
var sss = "test";
//If you want to close the Dialog when click the outside the Dialog part.
//this.Dismiss();
return true;
}

}

Following part is DialogFragment.

    public class MyDialog:Android.Support.V4.App.DialogFragment
{

public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

}

public override Dialog OnCreateDialog(Bundle savedInstanceState)
{

MyAlertDialog alertDialogBuilder = new MyAlertDialog(this.Activity);
alertDialogBuilder.SetTitle("MyDialog");
alertDialogBuilder.SetCanceledOnTouchOutside(true);
return alertDialogBuilder;
}

public override void OnPause()
{
base.OnPause();
}

public override void OnCancel(IDialogInterface dialog)
{
base.OnCancel(dialog);
}

public override void OnDismiss(IDialogInterface dialog)
{
base.OnDismiss(dialog);
}
}

Click the button to push the Dialog.

private void BtnClick_Click(object sender, System.EventArgs e)
{
MyDialog dialog = new MyDialog();
dialog.Show(SupportFragmentManager, "dialog");
}

AlertDialog disappears when touch is outside [Android]

Use setCanceledOnTouchOutside(false) for preventing the dismiss on touching outside of alert dialog.

setCancelable(false) is used for preventing the dismiss on pressing the back button.

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
Snooker_Scoreboard ss = new Snooker_Scoreboard();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(false);
builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
.setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getContext(),PlayerSelection.class);
startActivity(i);
}
});

// Create the AlertDialog object and return it
Dialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
return dialog;
}

How to prevent dismiss of alert dialog box when user click in background of it?

Just add alert.setCancelable(false);. The reason why it was not working (from the comment in code) is, you were using wrong method name before.

AlertDialog.Builder alert = new AlertDialog.Builder(...);

alert.setCancelable(false); // not SetCancelable(false). Case sensitive

});

How to prevent a dialog (alert) from closing when you touch outside or press back using Anko

Anko library of kotlin, provides the functionality to prevent dialog to close when press outside the dialog.. There is cancellable(BOOLEAN) method of alert to provide this functionality.

I have used the below lines of code to stop alert dialog to close.

alert("Testing alerts") {
title("Alert")
cancellable(false) ////SET TRUE/FALSE ACCORDING TO URS REQUIREMENT
positiveButton {
///PERFORM ANY TASK HERE
dismiss()
}
negativeButton {
dismiss()
}
}.show()

Don't allow to click outside dialog

Use this one:

new AlertDialog.Builder(this)
.setTitle("No internet")
.setCancelable(false)
.setMessage("Message ")
.setPositiveButton("Close", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int w) {
finish();
}
})
.show();


Related Topics



Leave a reply



Submit