Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change button text in alert dialog

Tags:

android

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();
}
like image 591
Native Nerd Avatar asked Mar 16 '14 11:03

Native Nerd


1 Answers

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;

    }

}
like image 125
ElDuderino Avatar answered Oct 01 '22 19:10

ElDuderino