0

I have alert dialog with an edit text added to it. It has positive and negative buttons. Whenever I click positive/ok button the dialog exits even if the edittext is empty.

I want to gain control over this behaviour. viz; I want the alertdialog to exit only if the edittext is not empty.

Note : strAuthorDesc is a String variable.

Here is the code that executes on click of a button.

                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setTitle("Author Desc.");
                final EditText descInput = new EditText(mActivity);
                descInput.setInputType(InputType.TYPE_CLASS_TEXT);
                descInput.setTextColor(ContextCompat.getColor(mActivity, R.color.black));
                descInput.setLines(5);
//                    descInput.setMaxLines(5);
                descInput.setGravity(Gravity.LEFT | Gravity.TOP);
                descInput.setSingleLine(false);
                descInput.setHorizontalScrollBarEnabled(false);
                builder.setView(descInput);

                builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        strAuthorDesc = descInput.getText().toString();
                        if(strAuthorDesc != null && !strAuthorDesc.equals("")){
                            dialog.dismiss();
                            Const.showToast("Description added", mActivity);
                            setAuthorDesc();
                        }else{
                            Const.showToast("Desc. cannot be empty!", mActivity);
                        }
                    }
                });
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

            alertDialog = builder.create();
            alertDialog.setCancelable(false);
            alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            alertDialog.show();
Chandan Pednekar
  • 525
  • 5
  • 19

4 Answers4

0

check if editext is empty or not

check below code for reference

EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
    Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
    return;
}

Check Below UPDATED Code From Your Code Code it will Work For You

                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                    builder.setTitle("Author Desc.");
                    final EditText descInput = new EditText(mActivity);
                    descInput.setInputType(InputType.TYPE_CLASS_TEXT);
                    descInput.setTextColor(ContextCompat.getColor(mActivity, R.color.black));
                    descInput.setLines(5);
    //                    descInput.setMaxLines(5);
                    descInput.setGravity(Gravity.LEFT | Gravity.TOP);
                    descInput.setSingleLine(false);
                    descInput.setHorizontalScrollBarEnabled(false);
                    builder.setView(descInput);

                    builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            strAuthorDesc = scInput.getText().toString();
                           if (strAuthorDesc.matches("")) {
                           dialog.dismiss();
                           Const.showToast("Description added",mActivity);
                           setAuthorDesc();
                           }else{
                              Const.showToast("Desc. cannot be empty!",mActivity);
            }
                        }
                    });
                    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();

                        }
                    });

                alertDialog = builder.create();
                alertDialog.setCancelable(false);
                alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                alertDialog.show();
Yogesh Borhade
  • 694
  • 1
  • 10
  • 24
0

I think I have figured it out. Now what happens is default state is to close the close the dialog when positive or negative button is pressed so we need to override the positive button and then do the validation there. here is the code below.

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Author Desc.");
        final EditText descInput = new EditText(this);
        descInput.setInputType(InputType.TYPE_CLASS_TEXT);
        descInput.setTextColor(ContextCompat.getColor(this, android.R.color.black));
        descInput.setLines(5);
//                    descInput.setMaxLines(5);
        descInput.setGravity(Gravity.LEFT | Gravity.TOP);
        descInput.setSingleLine(false);
        descInput.setHorizontalScrollBarEnabled(false);
        builder.setView(descInput);

        builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();

            }
        });

        alertDialog = builder.create();
        alertDialog.setCancelable(false);
        alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        alertDialog.show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                descInput.getText().toString();
                if(descInput.getText().toString().equals("")){
                    Log.d("Test","Is empty");


                }else{
                    Log.d("Test","Is Not Empty "+descInput.getText().toString());
                    alertDialog.dismiss();
                }
            }
        });

I hope this helps!

I have found the dialog related problem ans at this link How to prevent a dialog from closing when a button is clicked

Harsh Jain
  • 1,372
  • 1
  • 9
  • 17
0

Put builder.setCancelable(false); below your definition of alertDialog builder.

Then your positive / negative buttons

                    builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        strAuthorDesc = descInput.getText().toString();
                        if(strAuthorDesc != null && !strAuthorDesc.equals("")){

After dismissing the Dialog , don't call any method, call dismiss() in the last

                                dialog.dismiss();
                                Const.showToast("Description added", mActivity);
                                setAuthorDesc();
                            }else{
                                Const.showToast("Desc. cannot be empty!", mActivity);
                            }
                        }
                    });
                    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();

                        }
                    });
Danger
  • 433
  • 5
  • 17
0

I solved the problem using Dialog class instead of AlertDialog/Builder or extending DialogFragment or Dialog class.

Here is the code.

private void showDescDialog(boolean isEditingDesc, String textToEdit){
    final Dialog dialog = new Dialog(mActivity);
    dialog.setContentView(R.layout.custom_dialog_author_desc);
    dialog.setTitle("");

    TextView tvHeading = (TextView) dialog.findViewById(R.id.tv_heading);
    final EditText descInput = (EditText) dialog.findViewById(R.id.et_author_desc);
    Button btnSave = (Button) dialog.findViewById(R.id.btn_save);
    Button btnCancel = (Button) dialog.findViewById(R.id.btn_cancel);

    if(isEditingDesc){
        descInput.setText(textToEdit);
        tvHeading.setText("Edit author description");
    }else{
        tvHeading.setText("Add author description");
    }

    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            strAuthorDesc = descInput.getText().toString();
            if(!TextUtils.isEmpty(strAuthorDesc)){
                Const.showToast("Description added", mActivity);
                setAuthorDesc();
                dialog.dismiss();
            }else{
                Const.showToast("Desc. cannot be empty!", mActivity);
            }
        }
    });

    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.cancel();
        }
    });

    dialog.setCancelable(false);
    dialog.show();
}
Chandan Pednekar
  • 525
  • 5
  • 19