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

Anko Commons – Dialogs

Yash Thakkar edited this page Jun 19, 2017 · 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 {
    compile "org.jetbrains.anko:anko-commons:$anko_version"
}

Toasts

Simply shows a Toast message.

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

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

Clone this wiki locally