Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ExposedDropdownMenu's width match its widest MenuItem

I have a composable ExposedDropdownMenu similar to this.

ExposedDropdownMenuBox(
    expanded = expanded,
    onExpandedChange = {
        expanded = !expanded
    }
) {

    OutlinedTextField(
        readOnly = true,
        value = selected,
        onValueChange = { },
        trailingIcon = {
            ExposedDropdownMenuDefaults.TrailingIcon(
                expanded = expanded
            )
        }
    )

    ExposedDropdownMenu(
        expanded = expanded,
        onDismissRequest = {
            expanded = false
        }
    ) {
        options.forEach { selection ->
            DropdownMenuItem(
                onClick = {
                    selected = selection
                    expanded = false
                }
            ) {
                Text(text = selection)
            }
        }
    }
}

The resulting view looks like this:

enter image description here

I'm trying to reduce the width of the the dropdown so it actually wraps around its widest menu option.

And I've already tried setting modifiers at all levels of the composition to ensure no child is widening the parent, but to no success.

like image 580
ogtega Avatar asked Dec 21 '25 14:12

ogtega


1 Answers

Add Modifier.width(IntrinsicSize.Min) works for me:

ExposedDropdownMenu(
            modifier = Modifier.width(IntrinsicSize.Min),//Here
            expanded = expanded,
            onDismissRequest = { expanded = false },
        ) {

 //... items here 
}
like image 78
Campino Avatar answered Dec 24 '25 12:12

Campino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!