Using AlertDialog in Android

Continuing the work I based my post Popup Window in Android on, I actually later decided to not use the PopupWindow class as a means of notifying my users.

As a substitution I took a look at the AlertDialog class instead. When using the AlertDialog you don’t have to define a xml layout for it, so that way it’s less code to implement. But it also means it’s not as flexible of course.

It does however produce a stylish alert with a “stock android” look, which is good some times when more is less. So unless you really need to go all out and have full control of the layout of your popup, then maybe the AlertDialog can be of value for you as well.

Creating the AlertDialog

The AlertDialog is simple in it’s layout, it has a header with an optional icon, a main text, and between one and three buttons you can define.

In the code below I have created an AlertDialog with an icon in the header and with two buttons. One of the buttons has no click handler attached, which makes it just close to AlertDialog and return to the underlying Activity when clicked.

   private void initiateTwoButtonAlert(String displayText,
            String positiveButtonText, String negativeButtonText) {
        mDialog = new AlertDialog.Builder(this)
                .setTitle(getResources().getString(R.string.app_name))
                .setMessage(displayText)
                .setIcon(R.drawable.alert_icon)
                .setPositiveButton(positiveButtonText, delete_button_ok_click_listener)
                .setNegativeButton(negativeButtonText, null)
                .show();

        WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
        layoutParams.dimAmount = 0.9f;
        mDialog.getWindow().setAttributes(layoutParams);
        mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    }

Above I’m setting the AlertDialog’s title to the apps name. The message of the dialog is sent in to the method in the parameter displayText. The icon is set to my alert_icon that’s copied into my resource folder. Two buttons are defined, one of them with the click handler named delete_button_ok_click_listener, the code for that is not in the example but it’s just a basic click handler.

The result of the code can be seen in the image above.

Adding some flair to the AlertDialog

There is an added tweak in the code as well. Setting the FLAG_BLUR_BEHIND on the AlertDialog and changing the dimAmount on the layout parameters is not needed to get the AlertDialog up and running, but it adds a nice touch. What those rows add is blur on the background, setting the dimAmount simply lowers the light intensity, making the blurred background darker.

The value for dimAmount is between 0.0f and 1.0f(0.0f being full light intensity and 1.0f turning the background all black). A word of warning when using the AVD for development here, I’ve had issues in the emulator when setting dimAmount to 0.0f. Actually the emulator went all black after clicking on the AlertDialog and returning to the Activity.

Happy coding.


More ways to show information to users in Android

You can also use the PopupWindow, check out this article for an example.

Using AlertDialog as popup notifier
Tagged on:

8 thoughts on “Using AlertDialog as popup notifier

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.