Last Updated:

AlertDialog example in Android


A component (dialog box) is one of the important elements of the user interface in any application on Android. most often used in cases where we ask the user about something (permission / confirmation of the execution of some operation). In this tutorial, we'll look at creating a dialog box using different layouts.AlertDialogAlertDialog

Theory on AlertDialog in Android

AlertDialog is a basic component for building Android applications, so we need to have a good understanding of how it works and be able to create different layouts of dialog boxes.

In order to create in Android, we need to create an object that is a nested class. Let's look at an example:AlertDialogAlertDialogBuilderAlertDialog


Android AlertDialog layout

The layout of the dialog box allows you to work with several components:

Title is an optional element of the dialog box

Content — the text that will be shown to the user. This can be a message, a list or your completely custom layout.

Buttons. They are of three types: confirmation (OK), cancel, and an additional (custom) button. Any dialog box can contain a maximum of three action buttons.

Methods for working with dialog boxes

  • The method sets an icon (icon) in the dialog boxsetIcon(Drawable icon)
  • The method sets the option to cancel an action in the .setCancelable(boolean cancelable)AlertDialog
  • The method sets the message that will be displayed in the dialog box.setMessage(CharSequence message)
  • setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener) Specifies the list of items that will be displayed in the dialog box. the list item that the user selects will be tracked by the listener
  • After clicking on the Cancel button of the dialog box, the .setOnCancelListener (DialogInterface.OnCancelListener onCancelListener)
  • The method sets the header in . setTitle(CharSequence title)AlertDialog
  • The method gets the list type used in the dialog box.getListView()

Example of using AlertDialog in Android


Let's look at an example of working with . Let's start with the layout, in which we will place 3 buttons. Each button will bring up a dialog box with a different layout: with 1 button - OK, with two buttons - Cancel and OK, as well as a window with three options: OK, Cancel and the so-called neutral button.AlertDialog

Let's start with the layout of our app:activity_main.xml

As you can see from the layout, we have 3 nested Button widgets in the root .

Attributes as expected, are placed in the folder res/values/: RelativeLayoutandroid:textstrings.xml

Now initialize our widgets in code. We also need to put listeners on the buttons and click to call the desired . We will do this through the implementation of the interface , and the call to the desired dialog box will occur in the switch statement. The Listing of the MainActivity class is as follows:


As a result, we will get a combined result of clicking on each button. After clicking on each button, a pop-up window appears.