Overview of Alert Dialog and Dialog Fragments

Learn everything about alert dialogs and dialog fragments in Android.

Introduction

This lesson will go over AlertDialog and DialogFragment in Android. A Dialog element in Android refers to a small window that displays additional information to the user or prompts them to make a decision. Usually, a dialog doesn’t fill the screen and is used to force the user to take any action before proceeding.

The base class for dialogs in Android is Dialog, but using one of its subclasses—AlertDialog, DatePickerDialog, or TimePickerDialog—is recommended.

AlertDialog

An alert dialog can show a title, up to three buttons, a list of selectable items, or a custom user-defined layout.

To use an AlertDialog in a Fragment or Activity, we need to import the following class:

import androidx.appcompat.app.AlertDialog

Once we’ve added the import, we can display a simple AlertDialog using the following code snippet:

Get hands-on with 1200+ tech skills courses.