Composable Function

calculateWindowSizeClass

Calculates the window's WindowSizeClass for the provided activity.

AndroidWindowSizeClassSample

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
@Sampled
fun AndroidWindowSizeClassSample() {
    class MyActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                // Calculate the window size class for the activity's current window. If the window
                // size changes, for example when the device is rotated, the value returned by
                // calculateSizeClass will also change.
                val windowSizeClass = calculateWindowSizeClass(this)
                // Perform logic on the window size class to decide whether to use a nav rail.
                val useNavRail = windowSizeClass.widthSizeClass > WindowWidthSizeClass.Compact

                // MyScreen knows nothing about window size classes, and performs logic based on a
                // Boolean flag.
                MyScreen(useNavRail = useNavRail)
            }
        }
    }
}
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: