Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.88k stars 1.29k forks source link

Add spinner inside an alert #450

Open carlastabile opened 7 years ago

carlastabile commented 7 years ago

I'm trying to add a Spinner inside an alert but there's no example or documentation on how to use spinners. How to populate it with items?

This is my code so far

alert(getString(R.string.alert)) {
                positiveButton("Cool") { toast("Yess!!!") }
                customView {
                    linearLayout {

                        textView("I'm a text")
                        padding = dip(16)
                        orientation = LinearLayout.VERTICAL
                        spinner()

                    }
                }
            }.show()
noln commented 6 years ago

You can specify an ArrayAdapter as you would any other spinner in Kotlin.

Try this:

// Items for spinner
val items = arrayOf("One", "Two", "Three", "Four", "Five")

// Alert with a spinner
alert("Example Alert") {
            title = "Example Alert"
            customView {
                verticalLayout {
                    spinner {
                        adapter = ArrayAdapter(ctx, android.R.layout.simple_spinner_dropdown_item, items)
                    }
                }
            }
        }.show()