Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the contents of an Android dialog box after creation

Tags:

android

dialog

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.

like image 916
tronman Avatar asked Feb 08 '10 15:02

tronman


People also ask

How do I customize alert dialog?

Add custom_layout. xml in that activity in which you want to show custom alert dialog here it is added in MainActivity. java.

What's the difference between dialog and AlertDialog in android?

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 .


1 Answers

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;
    }
}
like image 59
Dave Webb Avatar answered Oct 13 '22 11:10

Dave Webb