Is there a simple way to change the contents of a dialog box in Android without having to re-create the dialog box? I know that Activity.onCreateDialog() is only called once when the dialog first needs to be created, and this is where you initially set the dialog's contents. I need to change the dialog's contents later, so I'm wondering what is the proper way to do this.
Add custom_layout. xml in that activity in which you want to show custom alert dialog here it is added in MainActivity. java.
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
The onPrepareDialog()
method is called just before each time the Dialog
is displayed allowing you to update it appropriately.
It's passed the same int
ID as onCreateDialog()
and the Dialog
that you created in that method.
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
//Always call through to super implementation
super.onPrepareDialog(id, dialog);
switch (id) {
case DIALOG_TIME:
((AlertDialog)dialog).setMessage("The time is " + new Date());
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With