0

I have an alert box, created through the following code:

private AlertDialog makeAndShowDialogBox(){

        myDialogBox = 

            new AlertDialog.Builder(this) 
            //set message, title, and icon
            .setTitle("Terminator") 
            .setMessage("Are you sure that you want to quit?") 
            .setIcon(android.R.drawable.btn_star)

            //set three option buttons
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int whichButton) { 
                 //whatever should be done when answering "YES" goes here


                    functionShow(myDialogBox);

                }              
            })//setPositiveButton
        /*  .setNeutralButton("Cancel", new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int whichButton) { 
                 //whatever should be done when answering "NO" goes here
                 msg = "Cancel " + Integer.toString(whichButton);   
                 txtMsg.setText(msg);
                }
                })*/


            .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int whichButton) { 
                 //whatever should be done when answering "NO" goes here
                 msg = "NO " + Integer.toString(whichButton);   
                 txtMsg.setText(msg);
             } 
            })//setNegativeButton

            .create();

            return myDialogBox;
    }

The alert box vanishes as soon as the user selects OK. But i want the alert box to still wait for a particular time before vanishing as i want to change the message on the alert box. How can i do it?

functionShow has following definition:

void functionShow(final AlertDialog dia)
    {

        dia.setMessage("Generating Unique ID ... Please Wait ");



        Handler handler = new Handler();
        handler.postDelayed(new Runnable() { 
            public void run() { 
                dia.setMessage("Notyfying the user ... Please Wait");         
            } 
       }, 10000); 


        Handler handler1 = new Handler();
        handler1.postDelayed(new Runnable () {
            public void run()
            {
                dia.setMessage("Your unique id is 0001. Kindly wait for the other person to respond");
            }
        }, 15000);

}

But the program checks out as soon as i click ok ... without showing other messages.

Rebooting
  • 2,762
  • 11
  • 47
  • 70
  • Seems pretty unintutive UI. If someone clicks to close something out, and it, instead, changes the text of the dialog, they may get confused. Can you show a Toast after the dialog closes? – Stealth Rabbi Dec 09 '11 at 13:54
  • @StealthRabbi: The message changes when the user clicks ok and tells the user to wait to perform some action – Rebooting Dec 09 '11 at 14:01
  • go to to add image and text you want to show in ur alert dialog box... http://stackoverflow.com/a/10861216/1428123 and edit in ur java code like below http://stackoverflow.com/a/10861174/1428123 – Amit Parjapati Jun 02 '12 at 09:42

2 Answers2

1

Use a custom AlertDialog like this:

MyAlertDialog.java

public class MyAlertDialog extends AlertDialog {

    public boolean allowedToDismiss = false;
    public void dismiss() {
        if(allowedToDismiss) {
            super.dismiss();
        }
    }
}

And inside the class where you hold your dialog:

MyAlertDialog myDialogBox;    
/*Code...*/

public void onClick(DialogInterface dialog, int whichButton) { 
     myDialogBox.allowedToDismiss = true;
     /*Code...*/
}
Caner
  • 57,267
  • 35
  • 174
  • 180
0

Just some guessing from my side:

In another thread with with some kind of thread.sleep in it?

Or in a asynctask with a while loop that's waiting for a result when it can proceed and close the diaolog?

Community
  • 1
  • 1
Dante
  • 1,104
  • 1
  • 10
  • 15