How to add message box with 'OK' button?

Rajkumar Reddy picture Rajkumar Reddy · Jun 7, 2011 · Viewed 209.1k times · Source

I want to display a message box with an OK button. I used the following code but it results in a compile error with argument:

AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
dlgAlert.setMessage("This is an alert with no consequence");
dlgAlert.setTitle("App Title");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();

How should I go about displaying a message box in Android?

Answer

Paresh Mayani picture Paresh Mayani · Jun 7, 2011

I think there may be problem that you haven't added click listener for ok positive button.

dlgAlert.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //dismiss the dialog  
        }
    });