Last active
April 16, 2020 13:29
-
-
Save diegohkd/9f2ceb6d291517d53fdd0951b5b394d9 to your computer and use it in GitHub Desktop.
Spinner with dropdown items that can be disabled and then not selectable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class City(val name: String, val isEnabled: Boolean) { | |
override fun toString(): String { | |
return name | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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