2

I am having some difficulties to update the content of an AlertDialog on the onPrepareDialog method.

I am setting the content of the AlertDialog, but the dialog comes to the screen with no buttons and no background. Probably the problem is related to the Builder.

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        return builder.create();
    default:
        return null;
    }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        createUserInformationAlertDialog(dialog);
        break;
    }
}

public void createUserInformationAlertDialog(Dialog dialogIn) {
    AlertDialog alertDialog = (AlertDialog) dialogIn;
    View dialoglayout = alertDialog.getLayoutInflater().inflate(
            R.layout.dialog_user_info,
            (ViewGroup) findViewById(R.id.dialog_user_layout_root));
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialoglayout);
    EventAttendant ea = this.event.getCrowd().getAttendees()
            .get(positionUserToHaveInformationDisplayedOnTheDialog);
    final EventAttendant clone = (EventAttendant) ea.clone();

        // Setting values
        TextView textView = (TextView) dialoglayout.findViewById(R.id.user_name_value);
        textView.setText(ea.getName());

        builder.setPositiveButton(Locale_PT_BR.SEE_ON_FACEBOOK,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {/* User clicked OK so do some stuff */
                    }
                });
        builder.setNegativeButton(Locale_PT_BR.BACK,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {...}
                });
        builder.setView(dialoglayout);
        alertDialog.setView(dialoglayout);
        alertDialog.setContentView(dialoglayout);
}
rlc
  • 5,809
  • 5
  • 38
  • 46
  • The problem is you can't just create another Builder in your custom onPrepareDialog() (which is where you were setting the buttons, instead of on the existing AlertDialog object) method and expect those parameters will just apply to the originally created Dialog. The better way to handle this is Pixie's answer, he just didn't explain much as to why yours broke. – devunwired Sep 01 '11 at 20:06

2 Answers2

2

You should create the dialog in onCreateDialog() and change text in onPrepareDialog().

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        return createUserInformationAlertDialog();

    default:
        return null;
    }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        prepareUserInformationAlertDialog((AlertDialog)dialog)
        break;
    }
}

public Dialog createUserInformationAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setPositiveButton(Locale_PT_BR.SEE_ON_FACEBOOK,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {/* User clicked OK so do some stuff */
                }
            });
    builder.setNegativeButton(Locale_PT_BR.BACK,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {...}
            });
    return builder.create();
}

public void prepareUserInformationAlertDialog(AlertDialog alertDialog) {
    EventAttendant ea = this.event.getCrowd().getAttendees()
            .get(positionUserToHaveInformationDisplayedOnTheDialog);
    final EventAttendant clone = (EventAttendant) ea.clone();

    View dialoglayout = alertDialog.getLayoutInflater().inflate(
            R.layout.dialog_user_info, null, false);
    // Setting values               
    TextView textView = (TextView) dialoglayout.findViewById(R.id.user_name_value);
    textView.setText(ea.getName());             
    alertDialog.setView(dialogLayout)
}

I haven't tested this code so it may contain some errors.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • you repeated the method names. The thing is a little bit more complicated because the positive button's action should also change. Can I set it on the `onPrepare` ? – rlc Sep 02 '11 at 16:06
  • Yes, you can do it using `alertDialog.setButton()` method. – Michael Sep 02 '11 at 18:33
1

You just need to call dialog.findViewById() in the onPrepareDialog() method and change the content of your views.

No need to set the whole layout again.

Working Example:

public class CustomDialogActivity extends Activity {

private Button button;
private final int DIALOG_1 = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DIALOG_1);
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case DIALOG_1:
        AlertDialog.Builder builder;

        LayoutInflater inflater = LayoutInflater.from(this);
        View layout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.layout_root));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText("Hello, this is a custom dialog!");

        builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        return builder.create();
    default:
        dialog = null;
    }
    return dialog;
}

@Override
protected void onPrepareDialog (int id, Dialog dialog) {
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("I've changed the text from when the dialog was built!");
}
}

And the XML 'dialog_layout.xml'

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp"
          >
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#FFF"
          />
</LinearLayout>
C0deAttack
  • 24,419
  • 18
  • 73
  • 81