calculateWindowSizeClass

Composable Function

Android
@ExperimentalMaterial3WindowSizeClassApi
@Composable
fun calculateWindowSizeClass(activity: Activity): WindowSizeClass

Calculates the window's WindowSizeClass for the provided activity.

A new WindowSizeClass will be returned whenever a configuration change causes the width or height of the window to cross a breakpoint, such as when the device is rotated or the window is resized.

Code Examples

AndroidWindowSizeClassSample

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
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)
            }
        }
    }
}