I want to create an AlertDialog with a timer in it. Basically I want the dialog to disappear if the user doesn’t make a decision on the buttons that are in the dialog after 30 seconds. I can create a dialog using an AlertDialog builder, but it seems like I cannot update the text using the .setMessage() method after the .show() method has been executed.
Any ideas on what I should do?
So in the code below if I run the .setMessage() method after the .show() method I see nothing. This is telling me that I cannot update the text in the dialog box in real time.
// Create the alert dialog with a alert builder.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm Settings Change")
.setCancelable(false)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
this.finish();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alert = builder.create();
alert.setMessage("test");
alert.show();
I think you need to have previously called
setMessage()at least once prior to callingshow()in order to be able to call it again afterwards. You can set it to an empty value or to a temporary one, then update it later: