Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/view/WindowMetrics#pubctors) \| [Methods](https://developer.android.com/reference/android/view/WindowMetrics#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/view/WindowMetrics#inhmethods)

# WindowMetrics

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/view/WindowMetrics "View this page in Kotlin") \|Java


`
public

final

class
WindowMetrics
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.view.WindowMetrics |

<br />

*** ** * ** ***

Metrics about a Window, consisting of the bounds and `https://developer.android.com/reference/android/view/WindowInsets`.


This is usually obtained from `https://developer.android.com/reference/android/view/WindowManager#getCurrentWindowMetrics()` and
`https://developer.android.com/reference/android/view/WindowManager#getMaximumWindowMetrics()`.
After `https://developer.android.com/reference/android/os/Build.VERSION_CODES#UPSIDE_DOWN_CAKE`, it also provides density.

### Obtains Window Dimensions in Density-independent Pixel(DP)


While `https://developer.android.com/reference/android/view/WindowMetrics#getDensity()` is provided, the dimension in density-independent pixel could also be
calculated with `WindowMetrics` properties, which is similar to
`https://developer.android.com/reference/android/content/res/Configuration#screenWidthDp`

```text
 float widthInDp = windowMetrics.getBounds().width() / windowMetrics.getDensity();
 float heightInDp = windowMetrics.getBounds().height() / windowMetrics.getDensity();
 
```
Also, the density in DPI can be obtained by:

```scdoc
 float densityDp = DisplayMetrics.DENSITY_DEFAULT * windowMetrics.getDensity();
 
```

<br />

<br />

**See also:**

- `https://developer.android.com/reference/android/view/WindowInsets#getInsets(int)`
- `https://developer.android.com/reference/android/view/WindowManager#getCurrentWindowMetrics()`
- `https://developer.android.com/reference/android/view/WindowManager#getMaximumWindowMetrics()`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/view/WindowMetrics#WindowMetrics(android.graphics.Rect,%20android.view.WindowInsets)(https://developer.android.com/reference/android/graphics/Rect bounds, https://developer.android.com/reference/android/view/WindowInsets windowInsets) ` *This constructor is deprecated. use `https://developer.android.com/reference/android/view/WindowMetrics#WindowMetrics(android.graphics.Rect,%20android.view.WindowInsets,%20float)` instead.* |
| ` https://developer.android.com/reference/android/view/WindowMetrics#WindowMetrics(android.graphics.Rect,%20android.view.WindowInsets,%20float)(https://developer.android.com/reference/android/graphics/Rect bounds, https://developer.android.com/reference/android/view/WindowInsets windowInsets, float density) ` The constructor to create a `https://developer.android.com/reference/android/view/WindowMetrics` instance. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/graphics/Rect` | ` https://developer.android.com/reference/android/view/WindowMetrics#getBounds()() ` Returns the bounds of the area associated with this window or `UiContext`. |
| ` float` | ` https://developer.android.com/reference/android/view/WindowMetrics#getDensity()() ` Returns the density of the area associated with this window or `UiContext`, which uses the same units as `https://developer.android.com/reference/android/util/DisplayMetrics#density`. |
| ` https://developer.android.com/reference/android/view/WindowInsets` | ` https://developer.android.com/reference/android/view/WindowMetrics#getWindowInsets()() ` Returns the `https://developer.android.com/reference/android/view/WindowInsets` of the area associated with this window or `UiContext`. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/view/WindowMetrics#toString()() ` Returns a string representation of the object. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### WindowMetrics

Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public WindowMetrics (Rect bounds, 
                WindowInsets windowInsets)
```


**This constructor is deprecated.**   

use `https://developer.android.com/reference/android/view/WindowMetrics#WindowMetrics(android.graphics.Rect,%20android.view.WindowInsets,%20float)` instead.

<br />

<br />

| Parameters ||
|---|---|
| `bounds` | `Rect`: This value cannot be `null`. <br /> |
| `windowInsets` | `WindowInsets`: This value cannot be `null`. <br /> |

### WindowMetrics

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public WindowMetrics (Rect bounds, 
                WindowInsets windowInsets, 
                float density)
```

The constructor to create a `https://developer.android.com/reference/android/view/WindowMetrics` instance.


Note that in most cases `https://developer.android.com/reference/android/view/WindowMetrics` is obtained from
`https://developer.android.com/reference/android/view/WindowManager#getCurrentWindowMetrics()` or
`https://developer.android.com/reference/android/view/WindowManager#getMaximumWindowMetrics()`.

<br />

<br />

| Parameters ||
|---|---|
| `bounds` | `Rect`: The window bounds. This value cannot be `null`. <br /> |
| `windowInsets` | `WindowInsets`: The `https://developer.android.com/reference/android/view/WindowInsets` of the window. This value cannot be `null`. <br /> |
| `density` | `float`: The window density <br /> |

## Public methods

### getBounds

Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Rect getBounds ()
```

Returns the bounds of the area associated with this window or `UiContext`.


**Note that the size of the reported bounds can have different size than
`https://developer.android.com/reference/android/view/Display#getSize(android.graphics.Point)` based on your target API level and calling context.**
This method reports the window size including all system
bar areas, while `https://developer.android.com/reference/android/view/Display#getSize(android.graphics.Point)` can report the area excluding navigation bars
and display cutout areas depending on the calling context and target SDK level. Please refer
to `https://developer.android.com/reference/android/view/Display#getSize(android.graphics.Point)` for details.


The following code snippet shows how to get the bounds excluding navigation bars and display
cutout:

```text
 final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
 // Gets all excluding insets
 final WindowInsets windowInsets = metrics.getWindowInsets();
 Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars()
         | WindowInsets.Type.displayCutout());

 int insetsWidth = insets.right + insets.left;
 int insetsHeight = insets.top + insets.bottom;

 // Legacy size that Display#getSize reports
 final Rect bounds = metrics.getBounds();
 final Size legacySize = new Size(bounds.width() - insetsWidth,
         bounds.height() - insetsHeight);
 
```

<br />

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/graphics/Rect` | window bounds in pixels. This value cannot be `null`. <br /> |

### getDensity

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public float getDensity ()
```

Returns the density of the area associated with this window or `UiContext`,
which uses the same units as `https://developer.android.com/reference/android/util/DisplayMetrics#density`.

<br />

| Returns ||
|---|---|
| `float` | <br /> |

**See also:**

- `https://developer.android.com/reference/android/util/DisplayMetrics#DENSITY_DEFAULT`
- `https://developer.android.com/reference/android/util/DisplayMetrics#density`

### getWindowInsets

Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public WindowInsets getWindowInsets ()
```

Returns the `https://developer.android.com/reference/android/view/WindowInsets` of the area associated with this window or
`UiContext`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/WindowInsets` | the `https://developer.android.com/reference/android/view/WindowInsets` of the visual area. This value cannot be `null`. <br /> |

### toString

Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |