Skip to content

Instantly share code, notes, and snippets.

@sivansundar
Last active November 7, 2023 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivansundar/09f2d47d75247c178c7629a94e35acf7 to your computer and use it in GitHub Desktop.
Save sivansundar/09f2d47d75247c178c7629a94e35acf7 to your computer and use it in GitHub Desktop.
val options = listOf("Bengaluru", "Delhi", "Bombay", "Calcutta")
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(options[0]) }
ExposedDropdownMenuBox(
modifier = Modifier.padding(
start = 32.dp,
end = 32.dp
),
expanded = expanded,
onExpandedChange = { expanded = !expanded },
) {
TextField(
// The `menuAnchor` modifier must be passed to the text field for correctness.
modifier = Modifier
.menuAnchor()
.fillMaxWidth()
.align(Alignment.CenterHorizontally),
readOnly = true,
value = selectedOptionText,
onValueChange = {},
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.textFieldColors(containerColor = Color.White),
shape = RoundedCornerShape(12.dp),
)
ExposedDropdownMenu(
modifier = Modifier.exposedDropdownSize(matchTextFieldWidth = true)
,
expanded = expanded,
onDismissRequest = { expanded = false },
) {
options.forEach { selectionOption ->
DropdownMenuItem(
text = { Text(selectionOption) },
onClick = {
selectedOptionText = selectionOption
expanded = false
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment