In Kotlin you can do as:
First, put this code in your layout
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
Then you can do in onCreate() in Activity as ->
val spinner = findViewById<Spinner>(R.id.spinner)
val items = arrayOf("500g", "1kg", "2kg")
val adapter = ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
items
)
spinner.setAdapter(adapter)
You can get listener from dropdown as:
spinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
arg0: AdapterView<*>?,
arg1: View?,
arg2: Int,
arg3: Long
) {
// Do what you want
val items = spinner.selectedItem.toString()
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
}