6

I've been messing with this for a few hours now, and just can't seem to find how to get it. Basically, I have an alert dialog for creating a password. There is a password field and a confirm password field. The listener checks if they match, and updates a textView. What I would like to do is change the text on the button from CANCEL to PROCEED when the two fields match. Everything is working up to that point...

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Build the dialog and set up the button click handlers
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Add variables
    final View layout = inflater.inflate(R.layout.fragment_setpassword, null);
    final EditText password1 = (EditText) layout.findViewById(R.id.EditText_Pwd1);
    final EditText password2 = (EditText) layout.findViewById(R.id.EditText_Pwd2);
    final TextView error = (TextView) layout.findViewById(R.id.TextView_PwdProblem);

    builder.setView(layout)
    .setTitle(R.string.hdrSetPassword)
    .setMessage(R.string.hdrSetPassword)
    .setPositiveButton(R.string.hdrSetPassword, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Send the positive button event back to the host activity
            String strPassword1 = password1.getText().toString();
            String strPassword2 = password2.getText().toString();
            if (strPassword1.equals(strPassword2)) {
                Register.password = password1.getText().toString();
                mListener.onDialogPositiveClick(CreatePasswordFragment.this);
                dialog.dismiss();
            } else{
                // Set password to empty so it fails checks
                Register.password = "";
                mListener.onDialogNegativeClick(CreatePasswordFragment.this);
                dialog.dismiss();
            }
        }
    });
    password2.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
              String strPass1 = password1.getText().toString();
              String strPass2 = password2.getText().toString();
              if (strPass1.equals(strPass2)) {
                 // CHANGE BUTTON TEXT TO "PROCEED" HERE
                 error.setText(R.string.txtPasswordsMatch);
              } else {
                // CHANGE BUTTON TEXT TO "CANCEL" HERE
                 error.setText(R.string.txtPasswordsDontMatch);
              }
        }            
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
     });

    return builder.create();
}
Native Nerd
  • 144
  • 1
  • 2
  • 8

1 Answers1

9

Did you try

Dialog dialog = builder.create();

and in afterTextChanged();

(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(yourString);

?

public class MenuFragment extends DialogFragment {

    private static final String VALID = "OK";
    private static final String INVALID = "Not OK";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final View layout = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
        final EditText et1 = (EditText) layout.findViewById(R.id.et1);
        final EditText et2 = (EditText) layout.findViewById(R.id.et2);

        builder.setView(layout)
        .setTitle(R.string.hello_world)
        .setMessage(R.string.hello_world)
        .setPositiveButton(INVALID, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Send the positive button event back to the host activity
                String strPassword1 = et1.getText().toString();
                String strPassword2 = et2.getText().toString();
                if (strPassword1.equals(strPassword2)) {
                    dialog.dismiss();
                } else{
                    dialog.dismiss();
                }
            }
        });

        final AlertDialog dialog = builder.create();

        TextWatcher watcher = new TextWatcher() {
            public void afterTextChanged(Editable s) {
                  String strPass1 = et1.getText().toString();
                  String strPass2 = et2.getText().toString();
                  if (strPass1.equals(strPass2)) {

                      dialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(VALID);

                  } else {

                      dialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(INVALID);

                  }
            }            
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
         };


        et1.addTextChangedListener(watcher);
        et2.addTextChangedListener(watcher);

        return dialog;

    }

}
Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
ElDuderino
  • 3,253
  • 2
  • 21
  • 38
  • Yes, I saw something like that on another answer. But, no matter where I put the "Dialog dialog = builder.create();" I get an error on the second one, "getButton is undefined"/"AlertDialog can not be resolved to a variable"/non-final variable error... If I tweak it to be `final AlertDialog dialog = builder.create();` and `dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText("Test");` it will run, but when triggered gives a NullPointerException error – Native Nerd Mar 16 '14 at 20:00
  • I made an edit to my answer, it contains a fully working example now, based on your code. – ElDuderino Mar 17 '14 at 10:09
  • Awesome man, thanks. I'm still not quite sure what I was screwing up. I think it was where I was defining the layout. But, when I just replaced the whole thing, works like a champ. Thanks again! – Native Nerd Mar 18 '14 at 07:52
  • @NativeNerd You should call `dialog.show()` then change texts – sma6871 Sep 17 '18 at 11:07