Composable Function

computedMediaQuery

Evaluates a query against the current UiMediaScope, wrapped in a computedStateOf.

ComputedMediaQuerySample

@Sampled
@Composable
fun ComputedMediaQuerySample() {
    Box(Modifier.fillMaxSize().padding(top = 100.dp)) {
        val showDualPane by computedMediaQuery { windowWidth >= 600.dp }
        val showNavigationRail by computedMediaQuery {
            windowWidth >= 500.dp && viewingDistance != ViewingDistance.Near
        }

        Column(Modifier.fillMaxSize()) {
            Row(Modifier.weight(1f).fillMaxWidth()) {
                if (showNavigationRail) {
                    // Show navigation rail
                    Box(Modifier.width(150.dp).background(Color.Red).fillMaxHeight())
                }

                // Actual content
                Box(Modifier.weight(1f).background(Color.Yellow).fillMaxHeight())

                if (showDualPane) {
                    // Split screen into 2 panes for additional content
                    Box(Modifier.weight(1f).background(Color.Cyan).fillMaxHeight())
                }
            }

            if (!showNavigationRail && !showDualPane) {
                // Show bottom navigation
                Box(Modifier.background(Color.Blue).height(80.dp).fillMaxWidth())
            }
        }
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: