Jetpack Compose Dialog - Is there a way to change the background transparency?

Viewed 2053

When we show a Dialog/AlertDialog in Jetpack Compose the background seems to be a bit dark, is there a way to adjust the background alpha or make it transparent? For eg: The White background in this image is turned into Dark grey when the dialog is shown.

2 Answers

This behavior is controlled by android.view.Window.

It lies deep under Dialog and the only way I can think of to change it is to copy all the source code.

Then, in this line of the source code, you can insert the following:

window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)

You could possibly use a full screen dialog and then insert a card into it with the text that you want. For instance:

AlertDialog(
            modifier = Modifier.fillMaxSize(),
            backgroundColor = Color.White.copy(alpha = 0.2f),
            properties = DialogProperties(usePlatformDefaultWidth = false),

You can add your Card Composable in the text part:

text = {
        Card(Modifier.size(200.dp)) {
        Text(text = "test")
     }
}

Hope this helps!

Related