https://kotlinlang.org logo
#compose
Title
# compose
c

caelum19

07/16/2020, 9:59 PM
Hey, I am trying to have a popup similar to AlertDialog but bigger and with different content,
Dialog
seems good for this but it appears to have a rather small fixed size? Any pointers?
t

Timo Drick

07/17/2020, 4:23 PM
I refactored the Dialog implementation to have fullscreen access. It is transparent overlay so you can decide for yourself how big the Dialog is (Could also take the whole screen):
Copy code
@Composable
fun FullScreenTransparentDialog(onCloseRequest: () -> Unit, children: @Composable() () -> Unit) {
    val view = ViewAmbient.current

    @OptIn(ExperimentalComposeApi::class)
    val recomposer = currentComposer.recomposer
    // The recomposer can't change.
    val dialog = remember(view) { DialogWrapperTransparent(view, recomposer, onCloseRequest) }
    dialog.onCloseRequest = onCloseRequest

    onActive {
        dialog.show()

        onDispose {
            dialog.dismiss()
            dialog.disposeComposition()
        }
    }

    onCommit {
        dialog.setContent {
            Box(Modifier.semantics { this.dialog = true }, children = children)
        }
    }
}

private class DialogWrapperTransparent(composeView: View,private val recomposer: Recomposer, var onCloseRequest: () -> Unit) : Dialog(composeView.context) {
    val frameLayout = FrameLayout(context)
    private var composition: Composition? = null
    init {
        window!!.apply {
            requestFeature(Window.FEATURE_NO_TITLE)

            setBackgroundDrawableResource(android.R.color.transparent)
            setFlags(0, WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

            val displayRect = Rect()
            decorView.getWindowVisibleDisplayFrame(displayRect) // This call is necessary to get the full screen. I do not know why :-(
            setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)
        }

        setContentView(frameLayout)
        ViewTreeLifecycleOwner.set(frameLayout, ViewTreeLifecycleOwner.get(composeView))
        ViewTreeViewModelStoreOwner.set(frameLayout, ViewTreeViewModelStoreOwner.get(composeView))
    }

    fun setContent(children: @Composable() () -> Unit) {
        composition = frameLayout.setContent(recomposer, children)
    }

    fun disposeComposition() {
        composition?.dispose()
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val result = super.onTouchEvent(event)
        if (result) {
            onCloseRequest()
        }
        return result
    }

    override fun cancel() {
        // Prevents the dialog from dismissing itself
        return
    }

    override fun onBackPressed() {
        onCloseRequest()
    }
}
c

caelum19

07/18/2020, 4:48 PM
Very cool, thanks for suaring!