Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack compose popup menu

I am trying to rewrite my project UI using Jetpack compose. Any idea to add popup menu using jetpack compose in android? like this one

I tried to implement it using Stack() layout but the results are not up to the mark.

@Composable
fun LiveDataComponentList(productList: List<Product>) {
    AdapterList(data = productList) { product ->
        Stack() {
            Clickable(onClick = { PopupState.toggleOwner(product) }) {
                Card(...) {...}

            if (PopupState.owner == product) {
                Surface(color = Color.Gray,modifier = Modifier.gravity(Alignment.TopEnd) + Modifier.padding(12.dp)) {
                    Column() {
                        Text("menu 1")
                        Text("menu 2")
                        Text("menu 3")
                        Text("menu 4")
                        Text("menu 5")
                    }
                }
            }
        }
    }
}

and PopupState is

@Model
object PopupState
{
    var owner:Product?=null
    fun toggleOwner(item:Product)
    {
        if(owner==item)
            owner=null
        else
            owner=item
    }
}

result is

screenshot

like image 381
resuser Avatar asked May 06 '20 08:05

resuser


3 Answers

With 1.0.0 you can use the DropdownMenu.
Something like:

var expanded by remember { mutableStateOf(false) }

DropdownMenu(
    expanded = expanded,
    onDismissRequest = { expanded = false }
) {
    DropdownMenuItem(onClick = { /* Handle refresh! */ }) {
        Text("Refresh")
    }
    DropdownMenuItem(onClick = { /* Handle settings! */ }) {
        Text("Settings")
    }
    Divider()
    DropdownMenuItem(onClick = { /* Handle send feedback! */ }) {
        Text("Send Feedback")
    }
}

enter image description here

like image 98
Gabriele Mariotti Avatar answered Oct 16 '22 07:10

Gabriele Mariotti


Since DropDownPopup was removed, I implemented one using DropDownMenu instead like this:

PopupMenu:

@Composable
fun PopupMenu(
    menuItems: List<String>,
    onClickCallbacks: List<() -> Unit>,
    showMenu: Boolean,
    onDismiss: () -> Unit,
    toggle: @Composable () -> Unit,
) {
    DropdownMenu(
        toggle = toggle,
        expanded = showMenu,
        onDismissRequest = { onDismiss() },
    ) {
        menuItems.forEachIndexed { index, item ->
            DropdownMenuItem(onClick = {
                onDismiss()
                onClickCallbacks[index]
            }) {
                Text(text = item)
            }
        }
    }
}

Toggle (thing to long click on to trigger PopupMenu):

@Preview
@Composable
fun Toggle() {
    var showMenu by remember { mutableStateOf(false) }

    PopupMenu(
        menuItems = listOf("Delete"),
        onClickCallbacks = listOf { println("Deleted") },
        showMenu = showMenu,
        onDismiss = { showMenu = false }) {
        Text(
            modifier = Modifier.clickable(onClick = {}, onLongClick = {
                showMenu = true
            }),
            text = "Long click here",
        )
    }
}
like image 29
AnT Avatar answered Oct 16 '22 06:10

AnT


After some research I found a solution to this, the key component is DropdownPopup

@Composable
fun LiveDataComponentList(productList: List<Product>) {
    AdapterList(data = productList) { product ->


        Clickable(onClick = { PopupState.toggleOwner(product) }) {
            Card(...) {...}
        }
        if (PopupState.owner == product) {
            DropdownPopup(dropDownAlignment = DropDownAlignment.End)
            {

                Surface(
                    shape = RoundedCornerShape(4.dp),
                    elevation = 16.dp,
                    color = Color.White,
                    modifier = Modifier.gravity(Alignment.End)+ Modifier.padding(end = 10.dp)
                )
                {
                    Column(modifier = Modifier.padding(10.dp)) {

                        MenuItem(text ="Edit", onClick = {})
                        MenuItem(text = "Delete", onClick = {})
                        MenuItem(text = "Details", onClick = {})


                    }
                }
            }
        }


    }
}

@Composable
fun MenuItem(text: String, onClick: () -> Unit) {
    Clickable(onClick = onClick, modifier = Modifier.padding(6.dp)) {
        Text(text = text, style = MaterialTheme.typography.subtitle1)

    }
}

This solution works fine with compose version dev10

like image 25
resuser Avatar answered Oct 16 '22 08:10

resuser