Skip to content

AlertDialogExtensions

BirjuVachhani edited this page Feb 13, 2019 · 1 revision

Examples

Dialog with two action buttons

alertDialog{
    title = "Dialog Title"
    message = "This is dialog message content. Isn't it cool?"
    positiveButton {
        text = "yes"
        onClick {
            // positive vibes only :)
        }
    }
    negativeButton {
        text = "no"
        onClick {
            // yeah, go and do negative stuff here :(
        }
    }
}

Text variables(title, message, text) are flexible. But they only like String and string resource type. Other than that will be thrown back to you as an Exception.

Using String resource

alertDialog {
    title = R.string.dialog_title
    message = R.string.dialog_message
    positiveButton {
        text = R.string.yes
        onClick {
            // positive vibes only :)
        }
    }
    negativeButton {
        text = R.string.no
        onClick {
            // yeah, go and do negative stuff here :(
        }
    }
}

alertDialog extension displays dialog by default and returns dialog object. Whereas createDialog just creates a dialog and return it. It's up to you when to display.

val dialog = createDialog { title = R.string.dialog_title message = R.string.dialog_message positiveButton { text = R.string.yes onClick { // yeah whatever, you're just creating it! } } negativeButton { text = R.string.no onClick { // yeah whatever, you're just creating it! } } }

Dialog with only one action button

Don't want negative button? then just don't set it. It's just that simple!

alertDialog {
    title = R.string.dialog_title
    message = R.string.dialog_message
    positiveButton {
        text = R.string.ok
        // Hell yeah, no negative stuff here :)
    }
}

Configuration

Want more control? There's a config function for you! config block lets you access builder for AlertDialog. What else do you need!

alertDialog {
    title = R.string.dialog_title
    message = R.string.dialog_message
    cancelable = false
    config {
        // Be careful, control is all yours.
    }
}

For more information, look at the AlertDialogExtensions.kt file.