WindowMetricsCalculator deprecated or what happen to WindowManager

Viewed 419

My problems is simple I am using this library

implementation "androidx.window:window:1.0.0"

before it was working well but now I can't import WindowMetricsCalculator, does it deprecated or what ??, there is alternative way to calculate WindowMetrics ??

private void computeWindowSizeClasses() {
        WindowMetrics metrics =  WindowMetricsCalculator.getOrCreate()
                .computeCurrentWindowMetrics(this);
}
1 Answers

If you're using Jetpack Compose, you can call this method: in androidx/compose/material3/windowsizeclass/AndroidWindowSizeClass.android.kt

@ExperimentalMaterial3WindowSizeClassApi
@Composable
fun calculateWindowSizeClass(activity: Activity): WindowSizeClass {
    // Observe view configuration changes and recalculate the size class on each change. We can't
    // use Activity#onConfigurationChanged as this will sometimes fail to be called on different
    // API levels, hence why this function needs to be @Composable so we can observe the
    // ComposeView's configuration changes.
    LocalConfiguration.current
    val density = LocalDensity.current
    val metrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(activity)
    val size = with(density) { metrics.bounds.toComposeRect().size.toDpSize() }
    return WindowSizeClass.calculateFromSize(size)
}

Related