Disable closing AlertDialog if EditText is empty

4.8k views Asked by At

I have alert dialog with EditText on it. If user press positive button and EditText is empty the alert dialog is closed. Now I want to disable closing if user leaves Edit Text empty. How to do that?

This is code for alert dialog:

@Override
        public void onClick(View v) {

            AlertDialog.Builder wndInput = new AlertDialog.Builder(
                    MainActivity.this);

            final EditText txtEditScraps = new EditText(MainActivity.this);
            txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
            wndInput.setTitle("Number of scraps:");
            wndInput.setCancelable(false);


            wndInput.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });

            wndInput.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {                                                                                                                                             
                        }
                    });

            wndInput.setView(txtEditScraps);
            wndInput.create().show();

        }
5

There are 5 answers

1
Moinkhan On BEST ANSWER

First of all do small changes on last 2 lines of your code..

wndInput.setView(txtEditScraps);
final AlertDialog alertDialog = wndInput.create();
alertDialog.show();

Means you just have reference the alert dialog.. and then add the following code.

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          Boolean wantToCloseDialog = (txtEditScraps.getText().toString().trim().isEmpty());
         // if EditText is empty disable closing on possitive button    
         if (!wantToCloseDialog)     
              alertDialog.dismiss();
      }
});
0
NavinRaj Pandey On

you can check a conditon inside onClick() method like this:

 wndInput.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {  

                       String etContent=txtEditScraps.getText();

                       if(!etContent.isEmpty()){

                                 wndInput.dismiss();
                                  }   
                        else{// do nothing or give editText is empty}                                                                                                                                        
                            }
                        });
0
Chandrakanth On

Try like this

  final AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("My dialog")
                .setPositiveButton(android.R.string.ok, null)
                .setNegativeButton(android.R.string.cancel, null)
                .create();

        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {

                Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "Now dialog will not close", Toast.LENGTH_LONG).show();
                        //alertDialog.dismiss();
                    }
                });
            }
        });
        alertDialog.show();
0
Shoeb Siddique On

You need to get the text of EditText, if length of EditText value is null then disable your setPositiveButton event.

You can do something like this.

   @Override
        public void onClick(View v) {

            AlertDialog.Builder wndInput = new AlertDialog.Builder(
                    MainActivity.this);

            final EditText txtEditScraps = new EditText(MainActivity.this);
            txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
            wndInput.setTitle("Number of scraps:");
            wndInput.setCancelable(false);


            wndInput.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });

            wndInput.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {        
                            String _edtValue = txtEditScraps.getText().toString().trim();
                            if (_edtValue.length()==0&&_edtValue==null&&_edtValue.equalsIgnoreCase("")) {
                                //do nothing.
                            }  else {
                                // dismiss your alert.
                            }
                        }
                    });

            wndInput.setView(txtEditScraps);
            wndInput.create().show();

        }
0
Ashish Vora On
@Override
public void onClick(View v) {

    AlertDialog.Builder wndInput = new AlertDialog.Builder(
            MainActivity.this);

    final EditText txtEditScraps = new EditText(MainActivity.this);
    txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
    wndInput.setTitle("Number of scraps:");
    wndInput.setCancelable(false);


    wndInput.setNegativeButton("CANCEL",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                 wndInput.dismiss();
                }
            });

    wndInput.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                if(txtEditScraps.getText().toString().trim()=="")    {
                    txtEditScraps.requestFocus(true);
                }
                else {
                     wndInput.dismiss();
                }
                }
            });

    wndInput.setView(txtEditScraps);
    wndInput.create().show();

}