Created
November 13, 2022 04:43
-
-
Save alexstyl/a75341dcff1669766e7edbfc0610c903 to your computer and use it in GitHub Desktop.
A quick and dirty way to do AutoCompleteTextViews in Jetpack Compose. Needs improvement in the UX (hide cursor when item selected) and nicer API and probably more.
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
var selectedOptionText by remember { mutableStateOf("") } | |
val options = listOf("Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread") | |
val filtered = options | |
.filter { it.lowercase().contains(selectedOptionText.lowercase()) } | |
val expanded = selectedOptionText.isNotEmpty() && filtered.isNotEmpty() | |
&& options.none { it.lowercase() == selectedOptionText.lowercase() } | |
ExposedDropdownMenuBox( | |
expanded = expanded, | |
onExpandedChange = {} | |
) { | |
TextField( | |
value = selectedOptionText, | |
onValueChange = { | |
selectedOptionText = it | |
}, | |
colors = ExposedDropdownMenuDefaults.textFieldColors() | |
) | |
ExposedDropdownMenu( | |
expanded = expanded, | |
onDismissRequest = {} | |
) { | |
filtered.forEach { selectionOption -> | |
DropdownMenuItem( | |
onClick = { | |
selectedOptionText = selectionOption | |
} | |
) { | |
Text(text = selectionOption) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment