WindowMetrics  |  API reference  |  Android Developers


public final class WindowMetrics
extends Object



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

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

After 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 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(Rect bounds, WindowInsets windowInsets)

This constructor is deprecated. use WindowMetrics(android.graphics.Rect, android.view.WindowInsets, float) instead.

WindowMetrics(Rect bounds, WindowInsets windowInsets, float density)

The constructor to create a WindowMetrics instance.

Public methods

Rect getBounds()

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

float getDensity()

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

WindowInsets getWindowInsets()

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

String toString()

Returns a string representation of the object.

Inherited methods

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(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 wait(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 wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

Public methods

getBounds

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 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 following code snippet shows how to get the bounds excluding navigation bars and display cutout:

 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);
 
Returns
Rect window bounds in pixels.
This value cannot be null.

getDensity

public float getDensity ()

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

Returns
float

toString

public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.