Added in API level 30

WindowMetrics


class WindowMetrics
kotlin.Any
   ↳ android.view.WindowMetrics

Metrics about a Window, consisting of the bounds and WindowInsets.

This is usually obtained from WindowManager#getCurrentWindowMetrics() and WindowManager#getMaximumWindowMetrics().

After android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE, it also provides density.

Obtains Window Dimensions in Density-independent Pixel(DP)

While getDensity() is provided, the dimension in density-independent pixel could also be calculated with WindowMetrics properties, which is similar to android.content.res.Configuration#screenWidthDp

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

Summary

Public constructors
WindowMetrics(bounds: Rect, windowInsets: WindowInsets)

WindowMetrics(bounds: Rect, windowInsets: WindowInsets, density: Float)

The constructor to create a WindowMetrics instance.

Public methods
Rect

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

Float

Returns the density of the area associated with this window or UiContext, which uses the same units as android.util.DisplayMetrics#density.

WindowInsets

Returns the WindowInsets of the area associated with this window or UiContext.

String

Public constructors

WindowMetrics

Added in API level 30
WindowMetrics(
    bounds: Rect,
    windowInsets: WindowInsets)

Deprecated: use WindowMetrics(android.graphics.Rect,android.view.WindowInsets,float) instead.

Parameters
bounds Rect: This value cannot be null.
windowInsets WindowInsets: This value cannot be null.

WindowMetrics

Added in API level 34
WindowMetrics(
    bounds: Rect,
    windowInsets: WindowInsets,
    density: Float)

The constructor to create a WindowMetrics instance.

Note that in most cases WindowMetrics is obtained from WindowManager#getCurrentWindowMetrics() or WindowManager#getMaximumWindowMetrics().

Parameters
bounds Rect: The window bounds This value cannot be null.
windowInsets WindowInsets: The WindowInsets of the window This value cannot be null.
density Float: The window density

Public methods

getBounds

Added in API level 30
fun getBounds(): Rect

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 Display#getSize(Point) based on your target API level and calling context. This method reports the window size including all system bar areas, while Display#getSize(Point) can report the area excluding navigation bars and display cutout areas depending on the calling context and target SDK level. Please refer to Display#getSize(Point) for details.

The value reported by Display#getSize(Point) excluding system decoration areas can be obtained by using:

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);
  

Return
Rect window bounds in pixels. This value cannot be null.

getDensity

Added in API level 34
fun getDensity(): Float

Returns the density of the area associated with this window or UiContext, which uses the same units as android.util.DisplayMetrics#density.

getWindowInsets

Added in API level 30
fun getWindowInsets(): WindowInsets

Returns the WindowInsets of the area associated with this window or UiContext.

Return
WindowInsets the WindowInsets of the visual area. This value cannot be null.

toString

Added in API level 30
fun toString(): String
Return
String a string representation of the object.