Loading...

Simple Android Alert Dialog

A dialog box is used to display message to the users, to ask users to enter some information and to take decision. In this post, I have created a simple android alert dialog box which shows a title of dialog, a text message and a button. Displaying an alert dialog is very easy but if you are beginner android developer, you may feel difficult for the first time.

You can also display your custom design alert dialog. Android Custom Alert Dialog Example gives you instructions to display custom alert dialog in your app. With custom alert dialog, you can easily use graphic and own design using xml like other layout.

Android Alert Dialog Example

How to Display Simple Android Alert Dialog Box


To display simple alert dialog box to your android application, simply use following java code in your onClick method.
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Alert Dialog Title");
        alertDialog.setMessage("Here is android alert dialog message");
        // Alert dialog button
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                       // Alert dialog action goes here
                       // onClick button code here
                        dialog.dismiss();// use dismiss to cancel alert dialog
                    }
                });
        alertDialog.show();
Here is modified code of activity layout file.
res/layout/activity_main.xml

Here is modified code of java activity file.
src/ActivityMain.java

Now, run your application and click Show Simple Android Alert Dialog button. Your application contains a dialog with some text message and a button like this.

Simple Android Alert Dialog
Alert Dialog Example
Tutorial 8688504692237279157
Home item