Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh or change the AlertDialog Message

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?

like image 553
Med Besbes Avatar asked Apr 19 '13 13:04

Med Besbes


3 Answers

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.

like image 52
stinepike Avatar answered Oct 05 '22 23:10

stinepike


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();
         }
      }
like image 14
Danger Avatar answered Oct 06 '22 01:10

Danger


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.

like image 7
android developer Avatar answered Oct 06 '22 00:10

android developer