Creating alerts and dialogs on Android is a relatively simple task thanks to the builder, but due to the verbosity of the language it usually becomes something quite illegible.
In Kotlin, by the mere fact of the lambdas, it simplifies us a little to understand what is happening in that code.
But thanks to Anko, we can make all this even easier, and in this article we’ll see how.
Alerts in Kotlin with Anko
Writing alerts with Anko is pretty easy. Just create an alert
block:
1 2 3 |
alert("Testing alerts") { ... }.show() |
Within the block, you can specify some things like the title of the alert, or the buttons that you want to appear
1 2 3 4 5 |
alert("Testing alerts") { title = "Alert" yesButton { toast("Yess!!!") } noButton { } }.show() |
This results in an alert like this:
You can customize the actions by using the positiveButton
, negativeButton
and neutralButton
methods:
1 2 3 4 5 6 |
alert("Testing alerts") { title = "Alert" positiveButton("Cool") { toast("Yess!!!") } negativeButton("Never Ever") { } neutralButton("I'll think about it") }.show() |
And even add a custom view that, of course, you can create also with Anko:
1 2 3 4 5 6 7 8 9 10 11 |
alert { title = "Alert" positiveButton("Cool") { toast("Yess!!!") } customView { linearLayout { textView("I'm a text") button("I'm a button") padding = dip(16) } } }.show() |
Progress Dialogs
Another interesting feature that includes Anko is to create progress dialogs, and indeterminate progress.
To give an example of the second ones, you can create a progress dialog in this simple way:
1 |
indeterminateProgressDialog("This a progress dialog").show() |
The result of the previous line will be this:
Conclusion
You see that thanks to Anko and the power of Kotlin, you can create small DSLs to simplify tasks. Ideally you could create DSLs on any part of the framework so that it makes interaction with it much easier.
In a later article we’ll see how to deal with SQLite databases.
Meantime, why not take a look at the free guide to learn how to build your first project? or in the book where you can learn how to create a complete App from scratch.
Author: Antonio Leiva
I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.
How do you avoid leaking when using the DSL? Does it take care of all lifecycle issues?
How can I hide the progress dialog?
The
alert
function returns a dialog. You can use it to calldismiss()
title(“Alert”) should be title=”Alert” for Android Studio 3
Oh, probably my fault and nothing to do with the Android Studio version. That’s a property in Anko library. Thanks, fixed it.
How can I implement OnDismissListener of the progress dialog?
Thank you for your articles and your book.
Something like this:
If I want to connect a xml file with the custom view, how can I do it in kotlin code… Greetings
Some time I don’t want the alert show immediately, Can I show the “alert” after certain seconds ? let say after .show(0.3s) or .show()after(0.4s)
You can insert that inside a
Handler().postDelayed()