Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to replace an AlertDialog's view after the dialog is shown?

E.g.

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(someView);
AlertDialog dialog = builder.create();
dialog.show();
... then later ...
dialog.setView(someOtherView);

Code executes with no error, but view is not replaced in the dialog. Am I doing it wrong or is this not possible?

like image 715
Mr. Bungle Avatar asked Nov 12 '22 12:11

Mr. Bungle


1 Answers

I'm not sure why but when using the alert dialog builder setView() does not work but setContentView() does

I think the alert dialog builder does not build the alert dialog in the conventional way in order to be compatible with all version of android so you will need to update the content view.

Dialog.setContentView(View view)

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();

then later

alertDialog.setContentView(newView);

It maybe easier to just rebuild a new alert dialog

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();

then later

alertDialog.dismiss();
AlertDialog.Builder updatedBuilder = new AlertDialog.Builder(context);
updatedBuilder.setView(updatedView);
AlertDialog updatedAlertDialog = builder.create();
updatedAlertDialog.show();
like image 195
Suphi Kaner Avatar answered Nov 15 '22 04:11

Suphi Kaner