AsfK
AsfK

Reputation: 3476

How to make live dialog?

I have to build a custom dialog that update the view when the content view is changed.

In the example below I have two TextView, one in the main activity and one in the dialog layout and counter from 1 to 10 should showed on both (main activity layout and dialog layout), but only the TextView in the main activity is update.

The code:

TextView tvCounter_Dialog, tvCounter_OutsideDialog;
int counter = 0;
Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_live_custom_dialog);
    handler = new Handler();

    View dialogView = getLayoutInflater().inflate(R.layout.dialog_live_layout, null);
    tvCounter_Dialog = (TextView) dialogView.findViewById(R.id.tvCounter);
    tvCounter_OutsideDialog = (TextView) findViewById(R.id.tvCounter_outsideDialog);

    Dialog counterDialog = new Dialog(this);
    counterDialog.setContentView(R.layout.dialog_live_layout);
    counterDialog.show();

    final Runnable updatedr = new Runnable() {
        @Override
        public void run() {
            counter = (counter + 1) % 10;
            tvCounter_Dialog.setText(String.valueOf(counter));
            tvCounter_OutsideDialog.setText(String.valueOf(counter));
            handler.postDelayed(this, 500);
        }
    };
    updatedr.run();
}

So the question is - how can I update the textview in the dialog?

EDIT: here is an capture (to be more clear)

enter image description here

Upvotes: 1

Views: 43

Answers (1)

Blackbelt
Blackbelt

Reputation: 157467

the mistake is on this line

 counterDialog.setContentView(R.layout.dialog_live_layout);

it should be

counterDialog.setContentView(dialogView);

in your version the custom view of your Dialog and the one you inflated are different

Upvotes: 1

Related Questions