11

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.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
AndroidDev
  • 5,193
  • 5
  • 37
  • 68

6 Answers6

15

You can make it easily like this:

Dialog(onDismissRequest = {}) {
    (LocalView.current.parent as DialogWindowProvider)?.window?.setDimAmount(0f)
    // dialog content here...
   }
Igor Konyukhov
  • 394
  • 4
  • 7
5

I was try with Dialog and no way to clear flag WindowManager.LayoutParams.FLAG_DIM_BEHIND.

You can try to use Popup to replace Dialog, everything work good for me.

Popup(
        onDismissRequest = {},
        properties = PopupProperties(
            focusable = true,
            dismissOnBackPress = false,
            dismissOnClickOutside = false,
            excludeFromSystemGesture = true,
        )
    ) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Transparent)
        ) {
            // Your content code is here
        }
    }
Cuong Le Q.
  • 61
  • 1
  • 2
2

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)
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
1

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!

Code Poet
  • 6,222
  • 2
  • 29
  • 50
  • Thanks but I don't want to create a new screen. I want to change the transparency of the current screen irrespective of its contents when a dialog is shown on top of it. – AndroidDev Apr 06 '22 at 19:55
0

Set decorFitsSystemWindows to false seems quite workable if you want to set transparent background.

Dialog(
    onDismissRequest = { /*TODO*/ },
    properties = DialogProperties(decorFitsSystemWindows = false)
) {

}
Andrii Turkovskyi
  • 27,554
  • 16
  • 95
  • 105
0

You might be able to achieve this by changing the window attributes. Here's how you can do it in a Dialog:

Dialog(
    onDismissRequest = {
        
    }
) {
    val curView = LocalView.current
    /* Change the transparency of the dialog window */
    LaunchedEffect(curView) {
        tailrec fun Context.findWindow(): Window? = when (this) {
            is Activity -> window
            is ContextWrapper -> baseContext.findWindow()
            else -> null
        }

        fun View.findWindow(): Window? =
            (parent as? DialogWindowProvider)?.window ?: context.findWindow()

        try {
            val window = curView.findWindow() ?: return@LaunchedEffect
            val lp = window.attributes
            lp.dimAmount = dimAmount // Modify the dim amount value
            window.attributes = lp
        } catch (e: Throwable) {
            e.printStackTrace()
        }
    }
    // content
}
midFang
  • 96
  • 3