-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Anko Commons – Dialogs
Dialog helpers are inside the anko-commons
artifact. Add it as a dependency to your build.gradle
:
dependencies {
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
}
Simply shows a Toast message.
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")
Simply shows a SnackBar message.
view.snackbar("Hi there!")
view.snackbar(R.string.message)
view.longSnackbar("Wow, such duration")
view.snackbar("Action, reaction", "Click me!") { doStuff() }
A small DSL for showing alert dialogs.
alert("Hi, I'm Roy", "Have you tried turning it off and on again?") {
yesButton { toast("Oh…") }
noButton {}
}.show()
The code above will show the default Android alert dialog. If you want to switch to the appcompat implementation, use the Appcompat
dialog factory:
alert(Appcompat, "Some text message").show()
Android
and Appcompat
dialog factories are included by default, but you can create your custom factories by implementing the AlertBuilderFactory
interface.
alert()
functions seamlessly support Anko layouts as custom views:
alert {
customView {
editText()
}
}.show()
selector()
shows an alert dialog with a list of text items:
val countries = listOf("Russia", "USA", "Japan", "Australia")
selector("Where are you from?", countries, { dialogInterface, i ->
toast("So you're living in ${countries[i]}, right?")
})
progressDialog()
creates and shows a progress dialog.
val dialog = progressDialog(message = "Please wait a bit…", title = "Fetching data")
An indeterminate progress dialog is also available (see indeterminateProgressDialog()
).