You've probably been browsing an application and had a little window pop up asking if you want to delete a file or confirming that you've submitted a form. Well, those little windows are dialog boxes, essential components that interrupt navigation so that the user can make a quick decision or enter some extra data without having to change screens.
The good news is that Android offers plenty of ways to do this, depending on whether you're working with the latest features or maintaining an older app. From the declarative approach of Jetpack Compose brings the classic view system to lifeWe have tools to create everything from a simple notification to a complex form that looks like an entire screen. Let's get started!
The modern approach: Dialogues with Jetpack Compose
If you're starting a project now, you'll most likely use Compose. Here things change quite a bit because we're no longer talking about inflating XML files, but about... define the interface using functionsThe star component is the composable one. AlertDialog, which already comes with the look and feel of Material Design 3.
To make a dialog appear and disappear, we can't simply call a method; we need to manage the reactive state of the interfaceFor this we use mutableStateOf y rememberBasically, we tell Android to store a variable (usually a boolean called showDialogand that, each time it changes, restructure the screen to show or hide the window.
In a AlertDialog Within Compose, we have several key sections: the onDismissRequestwhich is triggered when the user clicks outside the box or presses the back button; the title and descriptive text; and the action buttons. For the buttons, we usually use TextButtonwhere we define the action of confirming or cancelling, ensuring that change the state to false to close the dialogue at the end.
If you need something more advanced, like a list of items, you can insert a LazyColumn within the parameter textThis is the modern equivalent of the RecyclerView and allows the user Swipe and select options efficiently. And if the standard structure isn't enough for you, there's the composable one. Dialog It gives you carte blanche to use any layout, like a Card with columns and rows, achieving a total control over the design.
The traditional system: XML views and Kotlin
Despite the rise of Compose, the legacy system is still very much alive. The main tool here is... AlertDialog.BuilderUnlike Compose, this object works like a constructor where you add layers: title, message, and buttons. A critical point here is the context of the activityYou should never use getApplicationContext()because the dialog needs to know exactly which window it resides in in order to render correctly.
For the buttons, the Builder offers us three standard options: the positive button (to accept), the negative (to cancel) and the neutral (for actions like "remind me later"). Each one requires a DialogInterface.OnClickListener that defines what happens when the user clicks.
Lists and Selectors in the classic model
When we want the user to choose something, we have several options. If you only need a simple list, setItems() It's your best option. But if you're looking for something more interactive, you can use setSingleChoiceItems() to create radio buttons where only one option is valid, or setMultiChoiceItems() to deploy checkboxes that allow you to mark multiple preferences at once.
Deep customization with your own layouts
If the standard Android design falls short, you can create your own XML file in the folder res/layoutUsing the method setView(), Can inject your own design within the dialog. To handle the data in that layout (such as a login EditText), the best approach nowadays is to use View Bindingthus avoiding the tedious and error-prone process findViewById.
Advanced Management: DialogFragment and Activities
For more robust projects, simply launching a Builder isn't enough. Ideally, you should use a DialogFragmentThis class acts as a container that allows the dialogue survive configuration changes, such as when the user rotates the mobile screen. Instead of calling the dialog directly, we extend DialogFragment and we configured the AlertDialog within the method onCreateDialog().
If you need to send the dialog response back to the main activity, the cleanest way is to define a communication interfaceThe activity implements this interface, and the fragment calls it to notify that the user has pressed a specific button, thus maintaining a separation of responsibilities clear.
On very large screens, such as tablets, a small dialog box can look ridiculous. Android allows you to configure entire activities to behave like dialog boxes using the theme. Theme.Holo.DialogWhenLarge in the manifesto. This makes the make user experience adaptive, displaying the activity in full screen on mobiles and as a floating window on tablets.
Whether you opt for the agility of Jetpack Compose with its reactive states, or the robustness of DialogFragment and XML's MaterialAlertDialogBuilder, the key is to choose the tool that best suits your app's lifecycle and the device's screen size to ensure that the interaction is fluid and does not hinder the end user's navigation. Share the guide and help others learn about the topic.