I am trying to make an AlertDialog inside an AlertDialog, but when I run the code, secound AlertDialog is not showing
This is my code, I want to do it as if the user clicked the Cancel button in the first AlertDialog, the second AlertDialog appears, the user can enter his name and both metrics, and the name will go to ScoreActivity using intern.
void generateAlertDialog(final long timeSpent) {
AlertDialog.Builder builder = new AlertDialog.Builder(
GameActivity.this);
builder.setMessage(
"The time stayed in the game is " + timeSpent + "s")
.setTitle("You Lose!!")
.setPositiveButton("Replay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
Intent intent = new Intent(getApplicationContext(), GameActivity.class);
startActivity(intent);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
AlertDialog.Builder builder2 = new AlertDialog.Builder(
GameActivity.this);
builder2.setMessage(
"Please input you name")
.setTitle("Score");
final EditText input = new EditText(GameActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder2.setView(input);
builder2.setPositiveButton("Show Score LIst",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
Intent intent = new Intent(getApplicationContext(), ScoreActivity.class);
m_Text = input.getText().toString();
intent.putExtra("name", m_Text);
intent.putExtra("result", timeSpent);
startActivity(intent);
}
});
builder2.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
source
share