Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Anko Commons – Dialogs

rubenspischedda edited this page Jan 13, 2020 · 14 revisions

Contents

Using Anko Dialogs in your project

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
}

Toasts

Simply shows a Toast message.

toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")

SnackBars

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() }

Alerts

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()

Selectors

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?")
})

Progress dialogs

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()).