How to display a Yes/No dialog box on Android?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Confirmation dialogs are one of the most common UI patterns in Android apps. Whether you need to confirm a deletion, prompt a user to save changes, or ask for permission, a simple Yes/No dialog does the job. Android provides several ways to build one, and the best choice depends on your architecture and the UI toolkit you are using.
AlertDialog.Builder with Kotlin
The classic approach uses AlertDialog.Builder. You set a title, a message, and two buttons — one for the positive action and one for the negative action.
Setting setCancelable(false) prevents the user from dismissing the dialog by tapping outside of it, which is useful when you need an explicit answer.
AlertDialog.Builder with Java
The same pattern works in Java. The main difference is the lambda syntax for click listeners.
Both the Kotlin and Java versions produce the same result at runtime. The Kotlin version is shorter because SAM conversions turn the OnClickListener interface into a lambda automatically.
MaterialAlertDialogBuilder
If your app uses Material Design (and most modern apps should), replace AlertDialog.Builder with MaterialAlertDialogBuilder from the Material Components library. It follows the latest Material theming guidelines automatically.
Add the dependency in your build.gradle if you have not already:
MaterialAlertDialogBuilder is a drop-in replacement; every method available on AlertDialog.Builder works the same way.
DialogFragment for Lifecycle Safety
Creating an AlertDialog directly inside an Activity or Fragment can cause crashes if the dialog is still visible during a configuration change (such as a screen rotation). Wrapping the dialog in a DialogFragment ties it to the Android lifecycle so the system can recreate it automatically.
Show the dialog from your Activity or Fragment:
The caller listens for the result using the Fragment Result API, keeping the dialog decoupled from the host.
Jetpack Compose AlertDialog
In Compose-based UIs you do not use AlertDialog.Builder at all. Instead you use the AlertDialog composable, controlling visibility through state.
In the calling composable, manage the dialog state with remember:
The Compose approach is fully declarative — the dialog appears and disappears based on a boolean state value, which makes it easy to test and reason about.
Common Pitfalls
- Creating an
AlertDialogdirectly in an Activity without aDialogFragment, which leads to window-leaked exceptions during configuration changes like screen rotation. - Forgetting to call
show()at the end of the builder chain, resulting in a dialog that is constructed but never displayed. - Using
AlertDialog.Builderinstead ofMaterialAlertDialogBuilderin a Material-themed app, causing the dialog to look inconsistent with the rest of the UI. - Passing
nullas the click listener for the negative button instead of an explicit dismiss handler, which can confuse readers of the code about whether the tap is intentionally a no-op. - Managing Compose dialog visibility with a plain variable rather than a
MutableState, so recomposition never triggers and the dialog never appears.
Summary
- Use
AlertDialog.Builderfor simple Yes/No prompts in traditional View-based Android apps. - Prefer
MaterialAlertDialogBuilderto get Material Design theming out of the box. - Wrap dialogs in a
DialogFragmentto survive configuration changes and avoid lifecycle-related crashes. - In Jetpack Compose, use the
AlertDialogcomposable with boolean state to show and hide the dialog declaratively. - Always decide whether the dialog should be cancelable; set
setCancelable(false)oronDismissRequestdeliberately based on the required user experience.

