Dialogs in Android using Kotlin DSL

Bal sikandar
MindOrks
Published in
3 min readFeb 11, 2020

To understand how to create DSL in Kotlin you must go and read Part1 first. Now, this article will use learning from the last article to understand how to use Kotlin DSL to simplify Android Dialog API it’s usage.

DSL at the End

Let’s just look at what we’re creating before moving forward and build towards it.

dialog {
layoutId
= R.layout.layout_dialog
setCustomView = {it: View, dialog: DialogFragment ->

it.title.text = "This is Title"
it.description.text = "This is Description"

it.accept.setOnClickListener {
//do something here
}

it.reject.setOnClickListener {
//do something here
}

}
}

This Dialog DSL takes two parameters as inputs namely layoutId and setCustomView.

  • layoutId → you can pass any layout id here
  • setCustomView → here you can inflate views and perform actions.

Now if we look at below DSL from the previous article. You can see in Builder class we define all the properties that we’ll need for our DSL implementation like for above Dialog example: layoutId and setCustomView parameters

Init block

Inside the init block of our DSL Builder class we use the properties to implement the logic part. For example, In Dialog DSL we create a MyDialogFragment object and pass layoutId and we initialize a setCustomView callback.

init {
val frag = MyDialogFragment.newInstance(layoutId)
frag.setCustomView(setCustomView)
frag.show(
(alertContext as FragmentActivity).supportFragmentManager,
MyDialogFragment.TAG
)
}

Checkout the whole DialogDSLBuilder code here.

Inside Dialog Fragment

There are two things happening inside this DialogFragment. One we’re inflating the view using the layoutId we received.

inflater.inflate(layoutId!!, container, false)

Second, we initialize and invoke setCustomView callback with these parameters (inflated view + dialog reference) in this callback.

callback.invoke(view, this)

Check this link for the complete code of MyDialogFragment class and just like that we created a DSL for Dialog.

DSL method definition

We have implemented DSL methods in the DSL builder class inside the companion block.

inline fun Activity.dialog(block: Builder.() -> Unit) {
Builder(this).apply(block).build()
}

Just to be clear this block is the code block inside {} braces of Dialog DSL example above which we use to initialize Builder class using apply keyword.

block = {layoutId = “”, setCustomView =“”}

Why Dialog example?

In Android, we have to create a new Dialog class every time we want to show a dialog and then we’ll have to pass the data as parameters and implement an Interface to receive callbacks. I didn’t like this whole process and came up with this simple DSL solution.

With DSLs we can use Dialogs or BottomSheetDialogs just like any other views in our projects, not to forget we don’t have to copy-paste code every time we have to create a new Dialog.

For complete code and more examples check out the Repo.

Thanks for reading this article. Be sure to 👏 recommend this article if you found it helpful. It means a lot.

Also let’s connect on twitter, github and linkedin.

Clap, share if you like it and follow me for my next move.

--

--

Bal sikandar
MindOrks

Android developer @shuttl Ex @_okcredit. Blogger | Open source contributor https://about.me/balsikandar.