Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog with dynamically changing text on every request

I want to show an AlertDialog with one option that might change on every request. So for example at one time I want to show the option "add to contacts" while another time it should be "remove from contacts".

My code does work on the first time, however Android seems to cache the AlertDialog so that onCreateDialog is not executed next time. Therefore the option doesnt change anymore. Can I prevent this caching, or is there just another way of changing the option?

I am working with SDK 1.5 but using 1.1.

@Override
protected Dialog onCreateDialog(final int id) {
    ...
    String add_remove_contact = res.getString(R.string.profile_add_to_contacts);
    if (user.getContacts().contains(profileID)) {
        add_remove_contact = res.getString(R.string.profile_remove_from_contacts);
        // TODO: this string is not changed when contact status changes 
    }
    final CharSequence[] items = {res.getString(R.string.view_profile),
                                  res.getString(R.string.profile_send_message),
                                  add_remove_contact};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ...
    return builder.create();
}
like image 848
Ulrich Scheller Avatar asked Jun 05 '09 07:06

Ulrich Scheller


3 Answers

Take a look at onPrepareDialog method that will be called before dialog is shown. There You can change the required values based on request type.

Example with date picker

@Override
protected Dialog onCreateDialog(final int id) {
  switch (id) {
  case DIALOG_DATE_ID:
    final Calendar c = Calendar.getInstance();
    return new DatePickerDialog(this, this, c.get(Calendar.YEAR),
                                c.get(Calendar.MONTH), 
                                c.get(Calendar.DAY_OF_MONTH));
  default:
    return super.onCreateDialog(id);
  }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
  switch (id) {
  case DIALOG_DATE_ID:
    //update to current time
    final Calendar c = Calendar.getInstance();
    ((DatePickerDialog) dialog).updateDate(c.get(Calendar.YEAR), 
                                           c.get(Calendar.MONTH), 
                                           c.get(Calendar.DAY_OF_MONTH));
    break;
  }
}
like image 196
JaanusSiim Avatar answered Nov 11 '22 20:11

JaanusSiim


You can also use the removeDialog(int) function of the Activity. When a dialog is dismissed, the Activity basically stores the state of the dialog (for performance reasons I would imagine). Calling removeDialog(int) on the dialog forces the activity to unload all references for the dialog and dismisses it from the screen if it's being shown.

Creating Dialogs
Activity#removeDialog(int)

like image 32
Andrew Burgess Avatar answered Nov 11 '22 18:11

Andrew Burgess


This is a dup of this question: Android: Can not change the text appears in AlertDialog

You can also do it this way: http://andmobidev.blogspot.com/2010/03/modifying-alert-dialogs-list-items.html

Does seem to slow down the display of the longpress menu though...

like image 4
kenyee Avatar answered Nov 11 '22 19:11

kenyee