How to create a Custom Dialog box in android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A custom dialog is useful when a standard alert with a title, message, and two buttons is not enough for your flow. On modern Android, the safest implementation is usually a DialogFragment with a custom XML layout, because it handles lifecycle changes more cleanly than a raw Dialog.
Why DialogFragment Is the Better Starting Point
You can create dialogs directly with Dialog, but that approach is easy to misuse when activities rotate, fragments detach, or the process is backgrounded. DialogFragment integrates with the fragment lifecycle and makes showing, recreating, and dismissing dialogs more predictable.
That matters for real apps because dialogs are often shown during configuration changes, navigation transitions, or async callbacks.
Step 1: Define a Custom Layout
Create a layout file in res/layout/dialog_confirm_delete.xml:
This layout becomes the content view inside the dialog. Keep it focused and compact, because dialogs should support small screens and font scaling gracefully.
Step 2: Build the Dialog in a DialogFragment
Inflate the custom layout and attach it to an AlertDialog or MaterialAlertDialogBuilder.
This approach keeps the dialog self-contained and avoids tight coupling to a specific activity method.
Step 3: Show the Dialog and Receive the Result
From a fragment, register a result listener and show the dialog with the fragment manager.
Using the Fragment Result API keeps communication explicit and avoids deprecated callback patterns.
Input Validation Before Dismissal
If the positive action requires valid input, do not let the dialog dismiss immediately. Intercept the button click in onStart and only dismiss after validation succeeds.
This pattern is especially useful for forms, confirmations, and destructive actions.
Common Pitfalls
The most common mistake is showing a raw Dialog tied directly to an activity context and then hitting lifecycle problems during rotation or navigation.
Another issue is doing real work inside the dialog callback on the main thread. Button handlers should dispatch business logic, not perform long-running work directly.
Developers also forget string resources, accessibility labels, and input validation. A custom dialog is still part of your app UI, so it should meet the same quality bar as the rest of the interface.
Finally, avoid tightly coupling the dialog to one host activity through casts or hand-written interfaces when fragment results or a shared view model already solve the communication cleanly.
Summary
- Prefer
DialogFragmentover rawDialogfor lifecycle-safe custom dialogs. - Build the UI in a dedicated XML layout and inflate it inside the dialog.
- Return results with the Fragment Result API to keep components decoupled.
- Validate input before dismissing when the dialog contains form fields.
- Treat custom dialogs like normal UI: theme them well, use string resources, and test on small screens.
Related reading
- How to create a delay in Swift?
- How to create a delay in Swift?
- How to create a DialogFragment without title?
- How to create a GUID/UUID using iOS
- How to create a Looper thread, then send it a message immediately?
- How to create a multiline UITextfield?
- How to create a release signed apk file using Gradle?
- How to create a toast message in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.