androidandroid-dialogandroid-jetpack-compose

Jetpack Compose Fullscreen Dialog


I tried to make a fullscreen dialog using Jetpack Compose using this code:

Dialog(onDismissRequest = { /*TODO*/ }) {
           NewPostDialog()
       }

It ended up looking something like this. How can I remove the margin at the side (marked red)?

Example


Solution

  • UPDATE: As @Nestor Perez mentioned, since compose 1.0.0-rc01 you can set usePlatformDefaultWidthin DialogProperties to make a dialog fill the whole screenwidth:

    Dialog(
        properties = DialogProperties(usePlatformDefaultWidth = false),
        onDismissRequest...
        ){
          Surface(modifier = Modifier.fillMaxSize()) {
              DialogContent()
          }
        }
    

    Compose Dialog uses ContextThemeWrapper so you should be able to theme your dialog with a custom style.

    themes.xml:

     <style name="Theme.YourApp" parent="Theme.MaterialComponents.Light.NoActionBar">
           //theme content...
            <item name="android:dialogTheme">@style/Theme.DialogFullScreen</item>
        </style>
    
     <style name="Theme.DialogFullScreen" parent="@style/ThemeOverlay.MaterialComponents.Dialog.Alert">
            <item name="android:windowMinWidthMajor">100%</item>
            <item name="android:windowMinWidthMinor">100%</item>
        </style>
    

    And in code:

    @Composable
    fun FullScreenDialog(showDialog:Boolean, onClose:()->Unit) {
        if (showDialog) {
            Dialog(onDismissRequest =  onClose ) {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    shape = RoundedCornerShape(16.dp),
                    color = Color.LightGray
                ) {
                    Box(
                        contentAlignment = Alignment.Center
                    ) {
                        Text(modifier = Modifier.align(Alignment.TopCenter),
                            text = "top")
                        Text("center")
                        Text(
                            modifier = Modifier.align(Alignment.BottomCenter),
                            text = "bottom")
                    }
                }
            }
        }
    }