Last active
January 22, 2024 14:39
-
-
Save MachFour/369ebb56a66e2f583ebfb988dda2decf to your computer and use it in GitHub Desktop.
Jetpack Compose Material3 ActionMenu
Hi and thank you for the snippet!
I just found this script and wanted to pitch in as I had to make a few changes to make it work.
- With material3, line 73 should be:
DropdownMenuItem(onClick = item.onClick, text = { Text(item.name) })
- The DropdownMenuItem is not displayed at the correct position. A Box component should be added as a parent of the
IconButton
and theDropdownMenu
(line 63):
Box {
IconButton(onClick = { menuVisible.value = true }) {
Icon(Icons.Default.MoreVert, "More actions")
}
DropdownMenu(
expanded = menuVisible.value,
onDismissRequest = { menuVisible.value = false },
) {
// ...
}
}
- Here is how it can be used:
@Composable
fun DrawActionMenu() {
val items = listOf(
ActionItem(
R.string.refresh_action,
Icons.Default.Refresh,
OverflowMode.ALWAYS_OVERFLOW
) {
// trigger refresh
},
ActionItem(
R.string.settings,
Icons.Default.Settings,
OverflowMode.ALWAYS_OVERFLOW
) {
// show settings
},
)
ActionMenu(items)
}
@Preview
@Composable
fun PreviewActions() {
Surface {
Row(
modifier = Modifier.padding(all = 8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text("This is a title", modifier = Modifier.weight(1f))
DrawActionMenu()
}
}
}
Hi @romsahel, thanks for the suggestions!
- The Material3 updates are perfect, thanks. I've just updated my app to Compose Material3 and I'll publish my revisions.
- I tested this out and couldn't notice a difference adding the
Box
vs not adding it. It looks fine on my app, where the overflow icon is positioned next to the right (end) edge of the screen. Maybe it's been fixed in the Material3 library since you posted this? I'm currently on version 1.2.0-beta02.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @volo-droid, thanks for pointing that out. I hadn't updated the gist in a while, but I've just updated it with the version I'm currently using :)