androidx.compose.material3

ExposedDropdownMenuBoxExperimental

Composable function

An exposed drop-down menu is a text field with a menu under it. Use it in forms to pick one value from a list, like a size or a country.

It has three pieces, all inside the box:

  1. A text field with Modifier.menuAnchor(...). The menu opens under it.
  2. ExposedDropdownMenuDefaults.TrailingIcon to show the arrow.
  3. ExposedDropdownMenu with one DropdownMenuItem per option.

menuAnchor and ExposedDropdownMenu only exist inside the box.

A drop-down field to pick a value
A drop-down field to pick a value
val options = listOf("Small", "Medium", "Large")
var expanded by remember { mutableStateOf(true) }
var choice by remember { mutableStateOf(options[1]) }
Box(Modifier.size(300.dp, 300.dp)) {
    ExposedDropdownMenuBox(expanded = expanded, onExpandedChange = { expanded = it }) {
        OutlinedTextField(
            value = choice,
            onValueChange = {},
            readOnly = true,
            label = { Text("Size") },
            trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
            modifier = Modifier.menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable),
        )
        ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
            options.forEach { option ->
                DropdownMenuItem(text = { Text(option) }, onClick = { choice = option; expanded = false })
            }
        }
    }
}

Use ExposedDropdownMenuAnchorType.PrimaryNotEditable with a readOnly field to pick from a fixed list. Use PrimaryEditable if the user can also type to filter the list.

ParametersExperimental

@Composable
fun ExposedDropdownMenuBox(
    expanded: Boolean,
    onExpandedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    content: @Composable ExposedDropdownMenuBoxScope.() -> Unit,
)

expanded: BooleanRequired

Whether the menu is open. Pass your state here.

onExpandedChange: (Boolean) -> UnitRequired

Called when the user clicks the field. Set your state to the new value here.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

content: @Composable ExposedDropdownMenuBoxScope.() -> UnitRequired

The text field and the menu. See the three pieces above.