Create Custom Dialogs With DialogFragment in Android

Make your dialogs the way you want

M.N Emmanuel
CodeX

--

Photo by Volodymyr Hryshchenko on U

A dialog in programming is a view that prompts the user of some warning or request’s user to perform an action, in android, dialogs follow the same use-case and come in many types like Date picker, Time picker, Alert Dialog, but there is also the possibility to create and customize your dialogs

In this tutorial, I will show you how you can create your custom dialog for any use case you want in a step-by-step manner using Kotlin.

AlertDialog

In android, the Alert dialog is the simplest type of dialog. It usually consists of a warning and a button or two buttons which are usually Yes, No, or Ok.

Creating an alert dialog is very simple, the Kotlin code below does just that.

val builder = AlertDialog.Builder(requireActivity())
builder.setMessage("This is an Alert dialog in kotlin")
.setCancelable(false)
.setPositiveButton("ok") { dialog, id -> dismiss()}
val alert = builder.create()
alert.show()

DialogFragment

A DailogFragment in android is a subclass of the Fragment class but it specializes in creating dialogs, it is the key for creating any custom dialog of your choice.

--

--

M.N Emmanuel
CodeX
Writer for

Full stack Mobile Developer, with tech stack, Android, Kotlin, java, React, React Native, Flutter, Dart, C# Xamarin.