Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the content of a dialog box when onClick event occurs

In my application, I have created a dialog box to accept password. In the dialog a message, an EditText and two buttons (one +ve and one -ve) are present. When user clicks on the +ve button, I am checking whether the password entered in the EditText is valid or not. If not valid, I have to change the message of the dialog box prompting the user to re-enter the password again.

If the password is valid it works as per the expectation, but it is invalid, instead of updaing the message it simply closes the dialog.

I have already tried to solve it by overriding onPrepareDialog(), but it also doesn't work.

Here is my code:

protected Dialog onCreateDialog(int viewId){
    AlertDialog dialog = null;
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Enter Password To Continue");
            final EditText passwordField = new EditText(getApplicationContext());
            builder.setView(passwordField);
            builder.setPositiveButton("SUBMIT", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if(passwordField.getText().toString().equals("password")){
                        Intent intent = new Intent(MyFirstActivity.this, NextActivity.class);
                        finish();
                        startActivity(intent);
                    }else{
                            //Here I need to update the message
                        builder.setMessage("SORRY!!! Wrong Password Entered.\n Re-Enter Correct Password Again ");
                    }                       
                }
            });
            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            dialog =  builder.create();
    }
    return dialog;
}
like image 586
Chandra Sekhar Avatar asked Oct 19 '25 09:10

Chandra Sekhar


1 Answers

I think there are 2 issues here.

  1. If I recall correctly the default behavior of the PositiveButton on an AlertDialog will close the dialog. I think that is why yours is closing when the password is wrong. I think if you switch to

    builder.setNeutralButton(...);
    

    it will not close your dialog when pressed.

  2. You are changing the message on the Builder instead of the Dialog itself. I don't think the Builder will dynamically change the way the Dialog looks once it has already been created and shown.

    dialog.setMessage("WRONG PASSWORD");
    

will change it for you I think. If not then you'll have to make a TextView and add it to your Dialog with builder.setView(mTxt); then you can call setText(); on your reference and it will change the text on the screen.

I think a better solution than either might be a Toast pop-up though. The user has just typed a password and pressed submit so I think they can be counted on to be looking at the screen. Many other applications that have logins pages use Toast message to tell the user if they got the password wrong, so I don't think you'll be confusing your users or anything, it is a common design patter.

EDIT:

AlertDialog class definitely does have a setMessage() method. see the docs here. It is near the bottom of the docs page so it won't scroll it to the top when you click the link correctly. But it is there, 3rd one down from the top for me. Between setInverseBackgroundForced and setTitle

EDIT 2:

I implemented something similar to what you are trying to do a while ago. I had to use my own Custom Listener object with the dialog buttons I think in order to get them to not hide the Dialog upon being pressed.

Here is how I create and show the dialog:

  AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
  builder.setCancelable(false)
  .setMessage("Please Enter Something")

  .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
        //All of the fun happens inside the CustomListener now.
        //I had to move it to enable data validation.
        }
   });
   AlertDialog alertDialog = builder.create();
   alertDialog.show();
   Button theButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
   theButton.setOnClickListener(new CustomListener(alertDialog));

Then I created an Inner class inside my Activity called CustomListener like this:

class CustomListener implements View.OnClickListener {
    private final Dialog dialog;
    public CustomListener(Dialog dialog) {
        this.dialog = dialog;
    }
    @Override
    public void onClick(View v) {


        if(passwordField.getText().toString().equals("password")){
            //Password is correct.


        } else { //Password is incorrect.
            ((AlertDialog)dialog).setMessage("WRONG");
        }
    }
}

When you set the listener in this manner it will not close the Dialog automatically upon button press.

like image 158
FoamyGuy Avatar answered Oct 22 '25 00:10

FoamyGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!