# WindowMetricsCalculator

Artifact: [androidx.window:window](https://developer.android.com/jetpack/androidx/releases/window) [View Source](https://cs.android.com/search?q=file:androidx/window/layout/WindowMetricsCalculator.kt+class:androidx.window.layout.WindowMetricsCalculator) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/window#1.0.0)

*** ** * ** ***


```
interface WindowMetricsCalculator
```

<br />

*** ** * ** ***

An interface to calculate the `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` for an `https://developer.android.com/reference/android/app/Activity.html` or a `https://developer.android.com/reference/kotlin/androidx/annotation/UiContext`.

## Summary

| ### Public companion functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator` | `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#getOrCreate()()` |

| ### Public functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` | `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeCurrentWindowMetrics(android.app.Activity)(activity: https://developer.android.com/reference/android/app/Activity.html)` Computes the size and position of the area the window would occupy with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts. |
| `open https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` | `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeCurrentWindowMetrics(android.content.Context)(context: @https://developer.android.com/reference/kotlin/androidx/annotation/UiContext https://developer.android.com/reference/android/content/Context.html)` Computes the size and position of the area the window would occupy with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts. |
| `open https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics?` | `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeCurrentWindowMetricsOrNull(android.content.Context)(context: @https://developer.android.com/reference/kotlin/androidx/annotation/UiContext https://developer.android.com/reference/android/content/Context.html)` Returns the current `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` or `null` if `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeCurrentWindowMetrics(android.app.Activity)` fails. |
| `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` | `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeMaximumWindowMetrics(android.app.Activity)(activity: https://developer.android.com/reference/android/app/Activity.html)` Computes the maximum size and position of the area the window can expect with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts. |
| `open https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` | `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeMaximumWindowMetrics(android.content.Context)(context: @https://developer.android.com/reference/kotlin/androidx/annotation/UiContext https://developer.android.com/reference/android/content/Context.html)` Computes the maximum size and position of the area the window can expect with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts. |
| `open https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics?` | `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeMaximumWindowMetricsOrNull(android.content.Context)(context: @https://developer.android.com/reference/kotlin/androidx/annotation/UiContext https://developer.android.com/reference/android/content/Context.html)` Returns the maximum `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` or `null` if `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeMaximumWindowMetrics(android.app.Activity)` fails. |

## Public companion functions

### getOrCreate

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/window#1.0.0)

```
fun getOrCreate(): WindowMetricsCalculator
```

## Public functions

### computeCurrentWindowMetrics

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/window#1.0.0)

```
fun computeCurrentWindowMetrics(activity: Activity): WindowMetrics
```

Computes the size and position of the area the window would occupy with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts.

For example, `https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#layoutInDisplayCutoutMode` set to `https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS` or the `https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_LAYOUT_NO_LIMITS` flag set.

The value returned from this method may be different from platform API(s) used to determine the size and position of the visible area a given context occupies. For example:

- `https://developer.android.com/reference/android/view/Display.html#getSize(android.graphics.Point)` can be used to determine the size of the visible area a window occupies, but may be subtracted to exclude certain system decorations that always appear on screen, notably the navigation bar.

- The decor view's android.view.View#getWidth and android.view.View@getHeight can be used to determine the size of the top level view in the view hierarchy, but this size is determined through a combination of `https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html` flags and may not represent the true window size. For example, a window that does not indicate it can be displayed behind a display cutout will have the size of the decor view offset to exclude this region unless this region overlaps with the status bar, while the value returned from this method will include this region.

The value returned from this method is guaranteed to be correct on platforms `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q` and above. For older platforms the value may be invalid if the activity is in multi-window mode or if the navigation bar offset can not be accounted for, though a best effort is made to ensure the returned value is as close as possible to the true value. See .computeWindowBoundsP and .computeWindowBoundsN.

Note: The value of this is based on the last windowing state reported to the client.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/WindowManager.html#getCurrentWindowMetrics()` |   |
| `https://developer.android.com/reference/android/view/WindowMetrics.html#getBounds()` |   |

### computeCurrentWindowMetrics

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/window#1.1.0)

```
open fun computeCurrentWindowMetrics(context: @UiContext Context): WindowMetrics
```

Computes the size and position of the area the window would occupy with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts. The `https://developer.android.com/reference/android/content/Context.html` must either be a `https://developer.android.com/reference/kotlin/androidx/annotation/UiContext` or an Application `https://developer.android.com/reference/android/content/Context.html`. We recommend using a `https://developer.android.com/reference/kotlin/androidx/annotation/UiContext` as it will give the most accurate `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics`. Using the Application `https://developer.android.com/reference/android/content/Context.html` may exclude some decorations or can provide some unexpected behaviors for multi instance apps in a desktop like environment.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-not-implemented-error/index.html` | if not implemented. The default implementation from `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#getOrCreate()` is guaranteed to implement this method. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the `https://developer.android.com/reference/android/content/Context.html` is not a `https://developer.android.com/reference/kotlin/androidx/annotation/UiContext`. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeCurrentWindowMetrics(android.content.Context)` |   |

### computeCurrentWindowMetricsOrNull

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/window#1.6.0-alpha05)

```
open fun computeCurrentWindowMetricsOrNull(context: @UiContext Context): WindowMetrics?
```

Returns the current `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` or `null` if `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeCurrentWindowMetrics(android.app.Activity)` fails.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeCurrentWindowMetrics(android.app.Activity)` |   |

### computeMaximumWindowMetrics

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/window#1.0.0)

```
fun computeMaximumWindowMetrics(activity: Activity): WindowMetrics
```

Computes the maximum size and position of the area the window can expect with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts.

The value returned from this method will always match `https://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)` on `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q` and below.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/WindowManager.html#getMaximumWindowMetrics()` |   |

### computeMaximumWindowMetrics

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/window#1.1.0)

```
open fun computeMaximumWindowMetrics(context: @UiContext Context): WindowMetrics
```

Computes the maximum size and position of the area the window can expect with `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` width and height and any combination of flags that would allow the window to extend behind display cutouts.

The value returned from this method will always match `https://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)` on `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q` and below.

The `https://developer.android.com/reference/android/content/Context.html` must either be a `https://developer.android.com/reference/kotlin/androidx/annotation/UiContext` or an Application `https://developer.android.com/reference/android/content/Context.html`. We recommend using a `https://developer.android.com/reference/kotlin/androidx/annotation/UiContext` as it will give the most accurate `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics`. Using the Application `https://developer.android.com/reference/android/content/Context.html` may exclude some decorations or can provide some unexpected behaviors for multi instance apps in a desktop like environment.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-not-implemented-error/index.html` | if not implemented. The default implementation from `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#getOrCreate()` is guaranteed to implement this method. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the `https://developer.android.com/reference/android/content/Context.html` is not a `https://developer.android.com/reference/kotlin/androidx/annotation/UiContext`. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeMaximumWindowMetrics(android.content.Context)` |   |

### computeMaximumWindowMetricsOrNull

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/window#1.6.0-alpha05)

```
open fun computeMaximumWindowMetricsOrNull(context: @UiContext Context): WindowMetrics?
```

Returns the maximum `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetrics` or `null` if `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeMaximumWindowMetrics(android.app.Activity)` fails.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/window/layout/WindowMetricsCalculator#computeMaximumWindowMetrics(android.app.Activity)` |   |