Here is how to do it using Kotlin and Material TextInputLayout which can be used as Spinner replacement, first implement it like described in this answer: https://mcmap.net/q/235540/-how-to-add-floating-label-on-spinner
first define the enum like this:
enum class States(val label: String) {
AL("Alabama"),
AK("Alaska"),
AR("Arkansas"),
AZ("Arizona"),
CA("California"),
CO("Colorado"),;
override fun toString(): String {
return label
}
}
then set an adapter like this:
val items = OrderStatusType.values()
val adapter = ArrayAdapter(requireContext(), R.layout.spinner_item_tv, items)
autoCompleteTextView.setAdapter(adapter)
the spinner_item_tv could be any TextView, like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/spinner_item_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="@tools:sample/cities" />
to get the selected item:
val selectedState = autoCompleteTextView.text.toString()
you can also find the enum value by searching through them like this:
val selectedState = States.values().firstOrNull { it.label == autoCompleteTextView.text.toString() }
// could be null if nothing is selected