Search code examples
androidandroid-edittextpasswordsconfirmis-empty

Confirm password is empty in EditText in AlertDialog in Android


Here are my methods to 1st, enter a new password, and 2nd, to confirm the password.

public void editPassword(){

    AlertDialog.Builder d = new AlertDialog.Builder(this);
    d.setTitle("New password");

    // Set up the input
    final EditText newPassword = new EditText(this);
    // Specify the type of input expected as a password
    newPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    d.setView(newPassword);

    Log.d(DEBUG_TAG1,"new password: " + newPassword.getText().toString());
    final Editable changedPassword = newPassword.getText();

    // Set up the buttons
    d.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override
        public void onClick(DialogInterface dialog, int which) {

            confirmPassword(changedPassword);
        }
    });
    d.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    d.show();


}
public void confirmPassword(final Editable password){
    AlertDialog.Builder d = new AlertDialog.Builder(this);
    d.setTitle("Confirm password");

    // Set up the input
    final EditText confirmPassword = new EditText(this);
    // Specify the type of input expected as a password
    confirmPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    d.setView(confirmPassword);

    Log.d(DEBUG_TAG1,"Password: " + password.toString());
    Log.d(DEBUG_TAG1,"Confirm password: " + confirmPassword.getText().toString());
    final Editable confirm = confirmPassword.getText();

    // Set up the buttons
    d.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(password.equals(confirm)){
                Log.d(DEBUG_TAG1,"passwords match ");
                //TODO send new password to database
            }
            else{
                //TODO dialog/toast saying "passwords don't match"
                Log.d(DEBUG_TAG1,"passwords don't match ");
                confirmPassword(password);
            }

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

    d.show();

}

In the first method, editPassword(), the password is correctly stored in the variable changedPassword, and it is correctly passed to confirmPassword(changedPassword).

HOWEVER in the method confirmPassword(), the variable confirm is empty after the assignment final Editable confirm = confirmPassword.getText();

whats happening?? what am i missing here? i cant see the thing and it must be sth "stupid". Please, any help? Thx


Solution

  • Try this.

        d.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Editable confirm = confirmPassword.getText();
            if(password.equals(confirm)){
                Log.d(DEBUG_TAG1,"passwords match ");
                //TODO send new password to database
            }
            else{
                //TODO dialog/toast saying "passwords don't match"
                Log.d(DEBUG_TAG1,"passwords don't match ");
                confirmPassword(password);
            }
    
        }
    });