Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Messagebox

Tags:

android

android Messagebox doesn't show because of finish call, how to make this function wait for ok and then close

public void msbox(String str,String str2)
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);                      
    dlgAlert.setMessage(str2);
    dlgAlert.setTitle(str);              
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();
    finish(); 
}

should be like this

public void msbox(String str,String str2)
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);                      
    dlgAlert.setTitle(str); 
    dlgAlert.setMessage(str2); 
    dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
             finish(); 
        }
   });
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();
}
like image 719
User6996 Avatar asked Sep 17 '11 17:09

User6996


People also ask

What is alert box in android?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

What are dialogs in android?

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Dialog Design.

What are the dialog boxes that are supported in android?

1) AlertDialog 2) ProgressDialog 3) DatePickerDialog 4) TimePickerDialog. Android supports 4 types dialog boxes: AlertDialog : An alert dialog box supports 0 to 3 buttons and a list of selectable elements, including check boxes and radio buttons.


1 Answers

see SO question: AlertDialog doesn't wait for input

you will have to implement callback (OnClickListener) when user clicks OK on AlertDialog.

This all because Android dialog boxes are not modal (non-blocking invoker thread)

dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // call your code here
    }
});
like image 63
Marek Sebera Avatar answered Sep 17 '22 23:09

Marek Sebera