Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Dialog, keep dialog open when button is pressed

Tags:

android

dialog

I would like to keep my dialog open when I press a button. At the moment it's closing.

AlertDialog.Builder builder = new AlertDialog.Builder(this);  builder.setMessage("Are you sure you want to exit?")     .setCancelable(false)    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {        public void onClick(DialogInterface dialog, int id) {             MyActivity.this.finish();        }    })    .setNegativeButton("No", new DialogInterface.OnClickListener() {        public void onClick(DialogInterface dialog, int id) {             dialog.cancel();        }    }); AlertDialog alert = builder.create(); 
like image 519
Blareprefix Avatar asked May 26 '11 17:05

Blareprefix


People also ask

How do you prevent a dialog from closing when a button is clicked?

AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);

Can you have an alert dialog without any buttons if not why?

You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.

What is AlertDialog builder in Android?

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.


2 Answers

Yes, you can. You basically need to:

  1. Create the dialog with DialogBuilder
  2. show() the dialog
  3. Find the buttons in the dialog shown and override their onClickListener

So, create a listener class:

class CustomListener implements View.OnClickListener {   private final Dialog dialog;    public CustomListener(Dialog dialog) {     this.dialog = dialog;   }    @Override   public void onClick(View v) {      // Do whatever you want here      // If you want to close the dialog, uncomment the line below     //dialog.dismiss();   } } 

Then when showing the dialog use:

AlertDialog dialog = dialogBuilder.create(); dialog.show(); Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE); theButton.setOnClickListener(new CustomListener(dialog)); 

Remember, you need to show the dialog otherwise the button will not be findable. Also, be sure to change DialogInterface.BUTTON_POSITIVE to whatever value you used to add the button. Also note that when adding the buttons in the DialogBuilder you will need to provide onClickListeners - you can not add the custom listener in there, though - the dialog will still dismiss if you do not override the listeners after show() is called.

like image 184
Kamen Avatar answered Sep 20 '22 10:09

Kamen


Thanks Sogger for your answer, but there is one change that we have to do here that is, before creating dialog we should set possitive button (and negative button if there is need) to AlertDialog as traditional way, thats it.

Referenced By Sogger.

Here is the sample example ...

AlertDialog.Builder builder = new AlertDialog.Builder(this);         builder.setMessage("Test for preventing dialog close");         builder.setTitle("Test");          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 = true;                   //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.               }           }); 
like image 20
Shailesh Avatar answered Sep 19 '22 10:09

Shailesh