I am working with AlertDialog for user login to an application.I have displayed two edit views for enter user name and password and also I have used positive button and negative button on alert dialog.When the user entered invalid user name or password then click on ok i am displaying a text on same alert dialog with TexView but the alert dialog is closing.How can i stay with alert dialog till user enter valid details on edit text feilds?
i have implemented code for get the alert as follows:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Login here");
alert.setView(table);
alert.setPositiveButton("Login", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
objLogin.setLoginUsrName(userNameText.getText().toString());
objLogin.setLoginPassword(passwordText.getText().toString());
if(objLogin.userValid())
{
//getting next screen
}
else{
invalid.setText("Invalid user name or password");
invalid.setTextColor(Color.RED);
//diaplay same alert here with invalid text
}
}
});
alert.setNegativeButton("Register", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//get register screen code here
}
});
alert.show();
How can i show the alert till user entered valid details?
I think the problem is the positiveButton and negativeButton from the AlertDialog class. AlertDialog closes after pressing these. You might have better luck with a Dialog Builder instead of an AlertDialog, but I haven’t tested that.
Another fix:
Try adding two Android widgets to your ‘table’ layout, and setting an onclicklistener on them. Do this instead of the two default positive/negative buttons from the AlertDialog. This approach will also allow you to modify the placement and style of the buttons.