This document discusses different types of dialogs in Android, including alert dialogs, single-choice dialogs, dialogs with buttons, and custom dialogs. It explains how to create alert dialogs using the AlertDialog.Builder class and set title, content, and buttons. Code examples are provided to demonstrate creating single-choice, button, and custom dialogs by setting different components and layouts.
2. Android Dialogs Tutorial
Popups or dialogs are a common to prompt the user to take action. Android popups are called Dialogs. The base dialog class is extended by
AlertDialog class, which is extended by more specific dialog classes:
● DatePickerDialog
● ProgressDialog
● TimePickerDialog
In this tutorial we will create AlertDialogs using the AlertDialog.Builder helper class.
AlertDialogs are divided into the following components:
● Title Area
● Content Area
● Buttons - Positive, negative and neutral
None of the components are mandatory, and for each components there is a default view. In this tutorial we will see 3 examples:
● Single-Choice Dialog
● Dialog with buttons
3. Single-Choice Dialog
This example uses the following default components:
● Title Area - The default title view which is actually a TextView. The view is set using the setTitle() method of the AlertDialog.Builder class.
● Content Area - The default single-choice ListView which is actually a RadioGroup. The view is set using the setSingleChoiceItems() method of
the AlertDialog.Builder class.
The AlertDialog in this example is created using the following code:
1. builder = new AlertDialog.Builder(mContext);
2. builder.setTitle(R.string.dialog_single_choice_title);
3. builder.setSingleChoiceItems(R.array.color_names, mSingleChoicePosition, this);
4. mAlertDialog = builder.create();
5. mAlertDialog.show();
The color_names array is described in the res/values/color_names.xml file below:
1. <?xml version="1.0" encoding="utf-8"?>
2. <resources>
4. Dialog With Buttons
This example uses the following default components:
● Title Area - The default title view which is actually a TextView. The view is set using the setTitle() method of the
AlertDialog.Builder class.
● Content Area - The default message view which is actually a TextView. The view is set using the setMessage() method of the
AlertDialog.Builder class.
● Buttons - Positive and Negative buttons are set using the setPositiveButton() and setNegativeButton() methods of the
AlertDialog.Builder class.
The AlertDialog in this example is created using the following code:
1. builder = new AlertDialog.Builder(mContext);
2. builder.setTitle(R.string.dialog_with_buttons_title);
3. builder.setMessage(R.string.dialog_with_buttons_message);
4. builder.setPositiveButton(R.string.dialog_with_buttons_positive, this);
5. Custom Dialog
This example uses only the content component, for the following res/layout/custom_dialog.xml file is used:
1. <?xml version="1.0" encoding="utf-8"?>
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent" >
5.
6. <TextView android:id="@+id/tvTitle"
7. android:layout_width="match_parent"
8. android:layout_height="wrap_content"
9. android:padding="5dp"
10. android:text="@string/dialog_custom_title"
11. android:textAppearance="@android:style/TextAppearance.Medium.Inverse"