-1

I am trying to disable alert dialog close if TextView is Empty,I gone through few posts but non of them are custom Alert Dialog. How to solve this. Thanks in Advance Here is my code sample Custom Alert Dialog code

     protected void showInputDialog() {

    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(Nav_Settings.this);
    View promptView = layoutInflater.inflate(R.layout.nav_settings_change_password, null);
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Nav_Settings.this);
    alertDialogBuilder.setView(promptView);
    alertDialogBuilder.setIcon(R.drawable.icon_secret);

    alertDialogBuilder.setTitle("CHANGE PASSWORD");

    final EditText currentPassword = (EditText) promptView.findViewById(R.id.etCurrentPassword);
    final EditText newPassword = (EditText) promptView.findViewById(R.id.etNewPassword);
    final EditText confirmNewPassword = (EditText) promptView.findViewById(R.id.etConfirmNewPassword);

    // setup a dialog window
    alertDialogBuilder.setPositiveButton("Update Password", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            if (TextUtils.isEmpty(currentPassword.getText().toString().trim())) {
                currentPassword.setError("should not be empty");
            } else if (TextUtils.isEmpty(newPassword.getText().toString().trim())) {
                newPassword.setError("should not be empty");
            } else if (TextUtils.isEmpty(confirmNewPassword.getText().toString().trim())) {
                confirmNewPassword.setError("should not be empty");
            } else if (newPassword.getText().toString().trim() != confirmNewPassword.getText().toString().trim()) {
                confirmNewPassword.setError("new password not matching");
            } else if (currentPassword == newPassword) {
                newPassword.setError("new password should not be current password");
            } else {
                Toast.makeText(getApplicationContext(), currentPassword.getText().toString() + "\n" + newPassword.getText().toString() + "\n" + confirmNewPassword.getText().toString(), Toast.LENGTH_SHORT).show();

            }
        }
    }).setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
    alert.getButton(alert.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
    alert.getButton(alert.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
    Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/RobotoCondensed-Bold.ttf");
    alert.getButton(alert.BUTTON_POSITIVE).setTypeface(face);
    alert.getButton(alert.BUTTON_NEGATIVE).setTypeface(face);
}
Andi
  • 119
  • 2
  • 8

2 Answers2

0

Initially set it as disabled:

Button button = alert.getButton(alert.BUTTON_NEGATIVE);
button.setEnabled(false);
alert.setCancelable(false);

Add a TextWhatcher on your password EditText:

currentPassword.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(TextUtils.isEmpty(currentPassword.getText().toString().trim())){
                button.setEnabled(false);
                alert.setCancelable(false);
            } else{
                button.setEnabled(true);
                alert.setCancelable(true);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

Basically, each time the user changing the text in the EditText you can check if it's empty or not & enable/ disable the Cancel button accordingly.

Aishwarya Tiwari
  • 625
  • 4
  • 19
0

You can have a boolean at the top of the method and make it true only when all the validations are cleared

protected void showInputDialog() {

// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(Nav_Settings.this);
View promptView = layoutInflater.inflate(R.layout.nav_settings_change_password, null);
final boolean isCloseValid = false; // THIS BOOLEAN
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Nav_Settings.this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setIcon(R.drawable.icon_secret);

alertDialogBuilder.setTitle("CHANGE PASSWORD");

final EditText currentPassword = (EditText) promptView.findViewById(R.id.etCurrentPassword);
final EditText newPassword = (EditText) promptView.findViewById(R.id.etNewPassword);
final EditText confirmNewPassword = (EditText) promptView.findViewById(R.id.etConfirmNewPassword);

// setup a dialog window
alertDialogBuilder.setPositiveButton("Update Password", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        if (TextUtils.isEmpty(currentPassword.getText().toString().trim())) {
            currentPassword.setError("should not be empty");
isValidClose = false // VALIDATION FALSE
        } else if (TextUtils.isEmpty(newPassword.getText().toString().trim())) {
            newPassword.setError("should not be empty");
isValidClose = false // VALIDATION FALSE
        } else if (TextUtils.isEmpty(confirmNewPassword.getText().toString().trim())) {
            confirmNewPassword.setError("should not be empty");
isValidClose = false // VALIDATION FALSE
        } else if (newPassword.getText().toString().trim() != confirmNewPassword.getText().toString().trim()) {
            confirmNewPassword.setError("new password not matching");
isValidClose = false // VALIDATION FALSE
        } else if (currentPassword == newPassword) {
            newPassword.setError("new password should not be current password");
isValidClose = false // VALIDATION FALSE
        } else {
            Toast.makeText(getApplicationContext(), currentPassword.getText().toString() + "\n" + newPassword.getText().toString() + "\n" + confirmNewPassword.getText().toString(), Toast.LENGTH_SHORT).show();
isValidClose = true; // MAKE TRUE HERE 

        }
    }
}).setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
             if(isValidClose){ // if validations is cleared
               dialog.cancel();
              }
            }
        });

// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
alert.getButton(alert.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alert.getButton(alert.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
Typeface face = Typeface.createFromAsset(getAssets(),
        "fonts/RobotoCondensed-Bold.ttf");
alert.getButton(alert.BUTTON_POSITIVE).setTypeface(face);
alert.getButton(alert.BUTTON_NEGATIVE).setTypeface(face);

}

Wilson Christian
  • 650
  • 1
  • 6
  • 17