5

I'm using AlertDialog.Builder to build my dialog, it has an EditText which is necessary to be filled and I want to prevent closing the dialog while it is not. In the positive button's onClickListener I can check if the editText is filled or not but I don't know how to prevent closing...

builder.setPositiveButton("title", new DialogInterface.OnClickListener(){
     @Override
     public void onClick(DialogInterface dialog, int which) {
           if(...){
              //can close
           }else{
            //prevent closing
           }
     }
})
Md. Monsur Hossain Tonmoy
  • 11,045
  • 2
  • 22
  • 19
Krisztian
  • 373
  • 1
  • 3
  • 15
  • 6
    Possible duplicate of [How to prevent a dialog from closing when a button is clicked](https://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) – Ali_Waris Nov 28 '17 at 10:28

2 Answers2

42

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.
          }
      });
Antrromet
  • 15,294
  • 10
  • 60
  • 75
1

You can use setOnShowListener

AlertDialog.Builder sayWindows = new AlertDialog.Builder(
                MapActivity.this);
        final EditText saySomething = new EditText(MapActivity.this);
        sayWindows.setPositiveButton("ok", null);
        sayWindows.setNegativeButton("cancel", null);
        sayWindows.setAdapter(listWords, null);
        sayWindows.setView(saySomething);

        final AlertDialog mAlertDialog = sayWindows.create();
        mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {

                Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Do something
                       say = userName + " Says: "+saySomething.getText();
                       showPosition.setText(say); 
                    }
                });
            }
        });
        mAlertDialog.show();

source : Android Don't dismiss AlertDialog after clicking PositiveButton

Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30