Dialogs in Android are used to display short, focused interactions that require user input or confirmation. In modern Android development, dialogs should follow Material Design guidelines and be lifecycle-aware to prevent memory leaks and crashes.
This guide on javatechig.com explains how to implement:
- AlertDialog (Recommended approach)
- Custom Dialog with custom layout
- DialogFragment (Lifecycle-safe implementation)
- Best practices for modern Android (API 34+)
Understanding Dialogs in Modern Android
A dialog is a small window that prompts the user to make a decision or enter additional information.
Common use cases:
- Confirmation prompts (Delete, Exit, Logout)
- Form input
- Selection lists
- Error messages
⚠ Avoid using deprecated ProgressDialog. Use progress indicators inside layouts instead.
AlertDialog Example (Recommended Approach)
AlertDialog is the most commonly used dialog type.
Kotlin Implementation (Preferred)
val builder = AlertDialog.Builder(this)
builder.setTitle("Delete Item")
builder.setMessage("Are you sure you want to delete this item?")
builder.setPositiveButton("Yes") { dialog, _ ->
// Perform delete action
dialog.dismiss()
}
builder.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
val dialog = builder.create()
dialog.show()
Java Implementation
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete Item");
builder.setMessage("Are you sure you want to delete this item?");
builder.setPositiveButton("Yes", (dialog, which) -> {
dialog.dismiss();
});
builder.setNegativeButton("Cancel", (dialog, which) -> {
dialog.dismiss();
});
AlertDialog dialog = builder.create();
dialog.show();
Adding Material Design Styling (Recommended)
To use Material Design dialogs, ensure your app theme extends:
Theme.Material3.DayNight.NoActionBar
Then use:
MaterialAlertDialogBuilder(this)
Instead of AlertDialog.Builder.
Example:
MaterialAlertDialogBuilder(this)
.setTitle("Logout")
.setMessage("Do you really want to logout?")
.setPositiveButton("Yes") { dialog, _ ->
dialog.dismiss()
}
.setNegativeButton("No", null)
.show()
This follows official Material standards from Android guidelines.
Custom Dialog Example (Using Custom Layout)
Use a custom dialog when you need:
- Form input
- Custom UI
- Complex interactions
Step 1: Create Layout (dialog_custom.xml)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etName"
android:hint="Enter Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Step 2: Inflate and Show Dialog
val view = layoutInflater.inflate(R.layout.dialog_custom, null)
val editText = view.findViewById<EditText>(R.id.etName)
MaterialAlertDialogBuilder(this)
.setTitle("Input Required")
.setView(view)
.setPositiveButton("Submit") { dialog, _ ->
val name = editText.text.toString()
dialog.dismiss()
}
.setNegativeButton("Cancel", null)
.show()
DialogFragment (Lifecycle-Safe Implementation)
Using DialogFragment is the recommended approach for:
- Configuration changes (rotation)
- Fragment-based architecture
- Preventing window leaks
Create DialogFragment Class
class ConfirmDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(requireContext())
.setTitle("Confirm")
.setMessage("Proceed with action?")
.setPositiveButton("Yes") { dialog, _ ->
dialog.dismiss()
}
.setNegativeButton("Cancel", null)
.create()
}
}
Show DialogFragment
ConfirmDialog().show(supportFragmentManager, "ConfirmDialog")
This approach prevents WindowLeaked errors.
Dialog with List Items Example
val items = arrayOf("Option 1", "Option 2", "Option 3")
MaterialAlertDialogBuilder(this)
.setTitle("Choose Option")
.setItems(items) { dialog, which ->
val selected = items[which]
dialog.dismiss()
}
.show()
Common Mistakes to Avoid
1. Using Activity Context After Finish()
Always ensure:
!isFinishing
Before showing dialog in Activity.
2. Memory Leaks with Anonymous Classes
Prefer lifecycle-aware components like DialogFragment.
3. Blocking UI Thread
Never run heavy tasks inside dialog button listeners.
Best Practices (2026 Updated)
- Use MaterialAlertDialogBuilder for consistency
- Prefer DialogFragment for complex flows
- Avoid deprecated APIs
- Handle configuration changes properly
- Keep dialogs short and focused
- Use ViewBinding inside custom dialogs
- Do not overuse dialogs (UX guideline)
When to Use Dialog vs Bottom Sheet
Use Dialog when:
- Immediate confirmation required
- Small interaction
Use BottomSheet when:
- Multiple actions
- Rich UI interaction
- Modern UX preference


