Dialog — Display Messages
Imagine you try to delete a file and the app asks:
Are you sure you want to delete this file?
[YES] [NO]
This popup window that asks the user to take action is called a Dialog.
Unlike Toast, a Dialog requires user interaction.
1. Definition
A Dialog is a small popup window in Android that appears on top of the screen to show important information or ask the user to make a decision.
Simple meaning:
Dialog = A popup that asks the user to respond.
2. Purpose
Dialogs are used when the app needs attention or confirmation from the user.
Common uses:
| Situation | Dialog Example |
|---|---|
| Delete file | “Are you sure?” |
| Exit app | “Do you want to exit?” |
| Login error | “Invalid password” |
| Permission request | “Allow location access?” |
3. Types of Dialogs
Common dialog types in Android:
| Type | Purpose |
|---|---|
| AlertDialog | Show alerts and ask user decisions |
| ProgressDialog | Show loading progress |
| DatePickerDialog | Select date |
| TimePickerDialog | Select time |
The most commonly used dialog is AlertDialog.
4. Basic Syntax (Kotlin)
Example using AlertDialog.
val builder = AlertDialog.Builder(this)
builder.setTitle("Delete File")
builder.setMessage("Are you sure you want to delete this file?")
builder.setPositiveButton("Yes") { dialog, which ->
println("File Deleted")
}
builder.setNegativeButton("No") { dialog, which ->
dialog.dismiss()
}
builder.show()
Explanation:
setTitle()→ dialog titlesetMessage()→ dialog textsetPositiveButton()→ action buttonsetNegativeButton()→ cancel buttonshow()→ display dialog
5. XML Syntax
Dialogs are usually created in Kotlin, not XML.
But buttons triggering dialogs are in XML:
<Button
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete File"/>
6. Important Properties
| Property | Description |
|---|---|
| Title | Heading of dialog |
| Message | Information shown |
| Buttons | Actions like OK / Cancel |
| Cancelable | Allows dialog to close when clicking outside |
Example:
builder.setCancelable(false)
7. Important Methods
setTitle()
Sets dialog title.
builder.setTitle("Warning")
setMessage()
Sets message text.
builder.setMessage("Do you want to exit?")
setPositiveButton()
Adds positive action button.
builder.setPositiveButton("Yes") { dialog, which -> }
setNegativeButton()
Adds cancel button.
builder.setNegativeButton("No") { dialog, which -> }
show()
Displays dialog on screen.
builder.show()
8. Real-Time Examples
Example 1: Exit Confirmation
val builder = AlertDialog.Builder(this)
builder.setTitle("Exit App")
builder.setMessage("Do you want to exit?")
builder.setPositiveButton("Yes") { _, _ ->
finish()
}
builder.setNegativeButton("No") { dialog, _ ->
dialog.dismiss()
}
builder.show()
Output:
Exit App
Do you want to exit?
[YES] [NO]
Example 2: Delete Confirmation
builder.setTitle("Delete")
builder.setMessage("Delete this file?")
Used in:
File managers
Gallery apps
Email apps
Example 3: Login Error
builder.setTitle("Login Failed")
builder.setMessage("Incorrect username or password")
builder.setPositiveButton("OK", null)
builder.show()
Used in authentication screens.
Example 4: Internet Warning
No Internet Connection
[Retry] [Cancel]
Used in network-based apps.
9. Advantages
✔ Gets user's attention
✔ Allows user interaction
✔ Good for confirmations and warnings
✔ Improves user experience
10. Disadvantages
❌ Interrupts user workflow
❌ Overuse can annoy users
❌ Requires user action to close
11. One-Line Summary
Dialog is a popup window in Android that displays important information or asks the user to take an action.
✅ For exams and interviews, the 3 display message components are usually asked together:
| Component | Purpose |
|---|---|
| Log | Developer debugging messages |
| Toast | Short temporary message |
| Dialog | Popup requiring user action |