20

I create an AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
...
AlertDialog alert = builder.create();
alert.show();

After a moment I want to change the AlertDialog message without closing it.

Is it possible?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Med Besbes
  • 2,001
  • 6
  • 25
  • 38

4 Answers4

16

Agreed with android developer. You can also use

TextView messageView = (TextView) alert.findViewById(android.R.id.message);

To get the control over the messageTextView of AlertDialog. Then you can set the new text there.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 3
    This doesn't work. @Danger's answer is the correct one and it's nicer, just use alert.setMessage() instead of builder.setMessage() . – qwlice Jun 21 '17 at 13:04
16

Use alert.setMessage() instead of builder.setMessage(). Call alert.setMessage() and set message of your dialog anytime you want.

Example:

     AlertDialog.Builder dialogBuilder;
     AlertDialog alertDialog;

     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       dialogBuilder = new AlertDialog.Builder(MainActivity.this);
       alertDialog = dialogBuilder.create();
     }


     public void showAlert(int caller) {
       if(alertDialog != null && !alertDialog.isShowing()) {
        switch (caller){
            case 1:
                alertDialog.setMessage("First method call");
                break;
            case 2:
                alertDialog.setMessage("Second method call");
                break;
            case 3:
                alertDialog.setMessage("Third method call");
                break;
            }
            alertDialog.show();
         }
      }
Danger
  • 433
  • 5
  • 17
  • 6
    It is important to note that `alert.setMessage()` **won't work unless** at least you called his `AlertDialog.Builder` `builder.setMessage()` first with `""` input (`null` input won't work also). It is a known issue, see issue at [https://issuetracker.google.com/issues/36913966](https://issuetracker.google.com/issues/36913966) – Eido95 Jul 11 '17 at 18:50
  • Issue was originally reported before Android 2.3. Because of the many changes that existed in Android 4.x compared to previous versions, it's very likely that this issue doesn't exist in recent versions of Android like 4.2.2 or newer. – Danger Jul 12 '17 at 06:55
7

Yes ,you can.

For example, if you create your own dialog, with your own layout, you can set an id for each of the views, and then access each of them (for example the textView) and set its new text whenever you wish to.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 3
    For me it does not work: Using `TextView messageView = (TextView)dialog.findViewById(android.R.id.message); messageView.setText("MyText");` after having called `dialog.show()` does not change the text. – user905686 Dec 10 '15 at 19:24
  • Again, I wrote "with your own layout". For other solutions, they might not work well. – android developer Dec 10 '15 at 19:27
  • what if I need to edit the message of AlertDialog while the activity is paused. For example I show dialog asking the user to scan NFC Tag, and I need around 5 seconds to read the tag, how could I update the message from "scan the tag" to "Don't break the connection" – Karam Jul 24 '20 at 16:17
3

Set OnClickListener out of main setter like this:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
                        .setTitle(getResources().getString(R.string.dialog_title))
                        .setMessage("Waiting ...")
                        .setNeutralButton(R.string.check, null);

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

Button b_neutral = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
b_neutral.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TextView tv_message = (TextView) alertDialog.findViewById(android.R.id.message);
                        tv_message.setText("Works ...");
    }
});
Viktor
  • 111
  • 1
  • 1
  • 8