How to Prevent a Dialog from Closing When a Button Is Clicked

Prevent dialog from closing when positiveButton is clicked and condition not true

You can achieve it by overriding the OnClickListener of Positive Button as follows

        dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = editTextCode.getText().toString();
if(code.length() != 6 && !code.matches("[0-9A-F]+")){
//Don't dismiss
} else{
dialog.dismiss();
}
}
});

Note:

Remember to do it after dialog.show() is called, otherwise you will end up in getting NullPointerException.

Since you are using AppCompatDialogFragment do it as follows in onResume() of your ColorPicker

final AlertDialog dialog = (AlertDialog)getDialog();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = editTextCode.getText().toString();
if(code.length() != 6 && !code.matches("[0-9A-F]+")){
//Don't dismiss
} else{
dialog.dismiss();
}
}
});

How to prevent AlertDialog to close?

You can change the behavior of the button immediately after calling show() of the dialog, like this.

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
builder.setPositiveButton("Test",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do nothing here because we override this button later to change the close behaviour.
//However, we still need this because on older versions of Android unless we
//pass a handler the button doesn't get instantiated
}
});
AlertDialog dialog = builder.create();
dialog.show();
//Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dialog.dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});

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)

prevent closing dialog on click

I am guessing you are looking for a directive which will listen to the outside of the dialog clicks.
Here is my version:

@Directive({
selector: '[clickOutside]'
})
export class ClickOutsideDirective {
constructor(private elementRef: ElementRef) {
}

@Output()
clickOutside = new EventEmitter<Event>();

@HostListener('document:click', ['$event', '$event.target'])
onClick(event: MouseEvent, targetElement: HTMLElement): void {
if (!targetElement) {
return;
}

const clickedInside = this.elementRef.nativeElement.contains(targetElement);
if (!clickedInside) {
this.clickOutside.emit(event);
}
}
}

and use it as the following:

<div *ngIf="visible" class="overlay" (clickOutside)="visible=false">
....

DEMO

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: ...
)

Unable to get the button of the alert dialog to prevent the dialog from closing in Kotlin

You can't get the button on the builder, you need to get it from the dialog (which you create using the builder). Modify your code to:

val builder = AlertDialog.Builder(this)
with(builder) {
setTitle("Hello....?")
setCancelable(false)
setPositiveButton("Done", null)
setNegativeButton("Cancel", null)
val dialog = this.create()
dialog.show()
val positiveButton: Button = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
positiveButton.setOnClickListener {
}
}

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

});


Related Topics



Leave a reply



Submit