In this article, I am going to demonstrate using AlertDialog, which Prevent an AlertDialog from closing on Positive Button click.
Code:
public void ShowDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Sample Dialog");
builder.setMessage("Test for preventing dialog close");
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("OK", new OnClickListener()
{
@Override public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
} });
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
} });
final 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.
} });
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).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.
} });
}
Result:
Code:
public void ShowDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Sample Dialog");
builder.setMessage("Test for preventing dialog close");
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("OK", new OnClickListener()
{
@Override public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
} });
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
} });
final 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.
} });
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).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.
} });
}
Result:
thank you
ReplyDelete