Skip to content

Instantly share code, notes, and snippets.

@diegohkd
Last active April 16, 2020 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegohkd/9f2ceb6d291517d53fdd0951b5b394d9 to your computer and use it in GitHub Desktop.
Save diegohkd/9f2ceb6d291517d53fdd0951b5b394d9 to your computer and use it in GitHub Desktop.
Spinner with dropdown items that can be disabled and then not selectable
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/grey" android:state_enabled="false"/>
<item android:color="@color/black"/>
</selector>
data class City(val name: String, val isEnabled: Boolean) {
override fun toString(): String {
return name
}
}
class CityAdapter(
var cities: List<City>,
context: Context
) : ArrayAdapter<City>(
context,
android.R.layout.simple_spinner_item,
cities
) {
init {
setDropDownViewResource(R.layout.simple_spinner_dropdown_item)
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View =
(super.getDropDownView(position, convertView, parent) as TextView).apply {
val city = cities[position]
text = city.name
isEnabled = city.isEnabled
}
override fun isEnabled(position: Int): Boolean = cities[position].isEnabled
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupSpinner()
}
private fun setupSpinner() {
spinner.adapter = CityAdapter(
listOf(
City("Florianópolis", true),
City("Buenos Aires", false),
City("New York", true),
City("Toronto", false),
City("Halifax", false),
City("London", true),
City("Lisboa", true)
),
this
)
}
}
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:singleLine="true"
android:textColor="@drawable/background_selector" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment