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;
}
I think there are 2 issues here.
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
it will not close your dialog when pressed.
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.
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 callsetText();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
setInverseBackgroundForcedandsetTitleEDIT 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:
Then I created an Inner class inside my Activity called CustomListener like this:
When you set the listener in this manner it will not close the Dialog automatically upon button press.