Ashekur Rahman molla Asik Ashekur Rahman molla Asik -4 years ago 136
Android Question

Do not hide dialog when focus lost in Android

My simple code here shows a Dialog in Android. I want to not hide the dialog when the user clicks outside the dialog or the dialog loses focus. How can I do it?

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_LONG).show();
break;
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(getApplicationContext(), "no", Toast.LENGTH_LONG).show();
break;
}
}
};


And the button code is:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();


Thanks.

Answer Source

use this for hiding if clicked outside dialog.setCanceledOnTouchOutside(true);

or use this for NOT hiding if clicked out side dialog.setCanceledOnTouchOutside(false);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download