LoginSignup
7
7

More than 5 years have passed since last update.

【Android】Ankoでアラートダイアログを作る

Posted at

AndroidのUI構築用DSLライブラリの「Anko」でアラートダイアログ(Alert Dialog)を簡単に作れるので紹介です。

環境

  • kotlinバージョン: 1.1.3-2
  • ankoバージョン: 0.10.1

準備

gradleに以下を追記

dependencies {
    compile "org.jetbrains.anko:anko:$anko_version"
}

標準のダイアログ

Untitled.png

alert("Message") {
    title = "Title"
    yesButton {}
    noButton {}
}.show()

ボタンをカスタマイズしたダイアログ

Untitled.png

alert("Message") {
    title = "Title"
    positiveButton("Positive") {}
    negativeButton("Negative") {}
    neutralPressed("Neutral", {toast("Foo")})
}.show()

↑みたいにトーストも簡単にできる。

CustomViewを含んだダイアログ

Untitled.png

alert("Message") {
    title = "Title"
    yesButton {}
    noButton {}
    customView {
        linearLayout {
            padding = dip(16)
            textView("Input: ")
            editText {
                hint = "Hint text"
            }.lparams(width = matchParent)
        }
    }
}.show()

参考

Anko Commons – Dialogs · Kotlin/anko Wiki
https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-Dialogs

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7