Anko Commons – Dialogs

Alex Saveau edited this page Oct 29, 2017 · 7 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 {
    compile "org.jetbrains.anko:anko-commons:$anko_version"
    compile "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.

snackbar(view, "Hi there!")
snackbar(view, R.string.message)
longSnackbar(view, "Wow, such duration")
snackbar(view, "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()).

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.