Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack compose layout changes in dialog doesn't update the size

I've got a dialog where the user can select something and depending on the choice, the layout will be updated. The problem is that the height of the dialog is never updated to reflect the layout changes.

How to recompose the dialog to make the layout fit in the dialog?

Example:

@Composable
fun AlertDialogTest() {
    var showDialog by remember { mutableStateOf(false)}
    var showExtra by remember { mutableStateOf(false)}
    Button(onClick = { showDialog = true }) {
        Text("Open dialog")
    }
    if (showDialog) {
        AlertDialog(
            text = {
                Column { Button(onClick = {showExtra = true}) {
                        Text ("Show rest of dialog")
                    }
                    if (showExtra) {
                        Text("More text", style = MaterialTheme.typography.h5)
                        Text("Even more text", style = MaterialTheme.typography.h5)
                    }
                }
            },
            confirmButton = { TextButton(onClick = { showDialog = false }) {
                Text("Close")
            }},
            onDismissRequest = {showDialog = false},
        )
    }
}

And the result:

enter image description here

like image 955
Mackan Avatar asked Jul 21 '21 12:07

Mackan


People also ask

What is recomposition in jetpack compose?

Recomposition is when Jetpack Compose re-executes the composables that may have changed in response to state changes, and then updates the Composition to reflect any changes. A Composition can only be produced by an initial composition and updated by recomposition.

How do I get screen width on jetpack compose?

If you want to get the size in pixels: val screenDensity = configuration. densityDpi / 160f and multiply with dp, for example val screenHeight = configuration.

Is jetpack easier to compose?

Jetpack Compose is a modern declarative UI Toolkit for Android. Compose makes it easier to write and maintain your app UI by providing a declarative API that allows you to render your app UI without imperatively mutating frontend views.

Is jetpack compose the future of Android development?

Jetpack Compose comes with all the functionality needed to build a rich and responsive application UI and features full interoperability with current Android views, making it easy for developers to implement in existing projects.


Video Answer


2 Answers

It did work as expected for me in beta08. Then, after upgrate to rc02 it stopped working - popup dialogs, drop down menus (basicaly all elements that are backed by platform windows) stopped resizing properly on content size change. And indeed I've found a bug for it - https://issuetracker.google.com/issues/194911971. And for now I, sadly, haven't found a workaround, so we better wait until it's fixed.

UPD. As is commented in aforementioned issue, there is a workaround. Check the answer here.

like image 106
Jeffset Avatar answered Oct 19 '22 20:10

Jeffset


I've also faced the same issue, and found the solution for a longer text or other contents in Jetpack Compose Alert Dialog

 AlertDialog(
    onDismissRequest = { },
    properties = DialogProperties(usePlatformDefaultWidth = false),
    modifier = Modifier
        .padding(28.dp)
        .fillMaxWidth()
        .wrapContentHeight(),
    title = null,
    text = {
           // Your alert dialog content
    },confirmButton = {
        TextButton(onClick = { /*TODO*/ }) {
            Text(text = "OK")
        }
    })
like image 36
Murodjon Abdukholikov Avatar answered Oct 19 '22 19:10

Murodjon Abdukholikov