I am relatively new to Android / Java. Thanks to Stack Overflow, I was able to learn a lot from the questions asked here. However, I am now stuck in this problem.
I have a password entry AlertDialogthat appears when the application starts. It reads the password from EditTextand compares it with the one stored in the file. I need an extra AlertDialogone that displays when an invalid / invalid password is specified. I also implemented this.
Now this second dialog box has two buttons - Resetand Retry. I want the activity to display the first dialog again when we click Retry. This is where I am ignorant. If someone can provide a working solution and a little explanation, I will be very grateful.
Here is my code:
LayoutInflater li = LayoutInflater.from(context);
View passView = li.inflate(R.layout.authdialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(passView);
final EditText passInput = (EditText) passView.findViewById(
R.id.editTextDialogUserInput);
final TextView txtv = (TextView) findViewById(R.id.textv);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String data = "";
try {
FileInputStream fis = openFileInput("authfile");
InputStreamReader in = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(in);
data = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (data.toString().equals(
passInput.getText().toString())) {
txtv.setText("You Have Logged in");
} else {
LayoutInflater ln = LayoutInflater.from(context);
View invalidView = ln.inflate(R.layout.invdialog,
null);
AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(
context);
invalidDialogBuild.setView(invalidView);
invalidDialogBuild
.setCancelable(false)
.setPositiveButton(
"Retry",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
})
.setNegativeButton(
"Reset",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog.cancel();
AlertDialog invalidDialog = invalidDialogBuild
.create();
invalidDialog.show();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
source
share