Skip to content

Most visited

Recently visited

navigation
Added in API level 1

DisplayMetrics

public class DisplayMetrics
extends Object

java.lang.Object
   ↳ android.util.DisplayMetrics


A structure describing general information about a display, such as its size, density, and font scaling.

To access the DisplayMetrics members, initialize an object like this:

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);

Summary

Constants

int DENSITY_280

Intermediate density for screens that sit between DENSITY_HIGH (240dpi) and DENSITY_XHIGH (320dpi).

int DENSITY_360

Intermediate density for screens that sit somewhere between DENSITY_XHIGH (320 dpi) and DENSITY_XXHIGH (480 dpi).

int DENSITY_400

Intermediate density for screens that sit somewhere between DENSITY_XHIGH (320 dpi) and DENSITY_XXHIGH (480 dpi).

int DENSITY_420

Intermediate density for screens that sit somewhere between DENSITY_XHIGH (320 dpi) and DENSITY_XXHIGH (480 dpi).

int DENSITY_560

Intermediate density for screens that sit somewhere between DENSITY_XXHIGH (480 dpi) and DENSITY_XXXHIGH (640 dpi).

int DENSITY_DEFAULT

The reference density used throughout the system.

int DENSITY_HIGH

Standard quantized DPI for high-density screens.

int DENSITY_LOW

Standard quantized DPI for low-density screens.

int DENSITY_MEDIUM

Standard quantized DPI for medium-density screens.

int DENSITY_TV

This is a secondary density, added for some common screen configurations.

int DENSITY_XHIGH

Standard quantized DPI for extra-high-density screens.

int DENSITY_XXHIGH

Standard quantized DPI for extra-extra-high-density screens.

int DENSITY_XXXHIGH

Standard quantized DPI for extra-extra-extra-high-density screens.

Fields

public float density

The logical density of the display.

public int densityDpi

The screen density expressed as dots-per-inch.

public int heightPixels

The absolute height of the display in pixels.

public float scaledDensity

A scaling factor for fonts displayed on the display.

public int widthPixels

The absolute width of the display in pixels.

public float xdpi

The exact physical pixels per inch of the screen in the X dimension.

public float ydpi

The exact physical pixels per inch of the screen in the Y dimension.

Public constructors

DisplayMetrics()

Public methods

boolean equals(Object o)

Compares this instance with the specified object and indicates if they are equal.

boolean equals(DisplayMetrics other)

Returns true if these display metrics equal the other display metrics.

int hashCode()

Returns an integer hash code for this object.

void setTo(DisplayMetrics o)
void setToDefaults()
String toString()

Returns a string containing a concise, human-readable description of this object.

Inherited methods

From class java.lang.Object

Constants

DENSITY_280

Added in API level 22
int DENSITY_280

Intermediate density for screens that sit between DENSITY_HIGH (240dpi) and DENSITY_XHIGH (320dpi). This is not a density that applications should target, instead relying on the system to scale their DENSITY_XHIGH assets for them.

Constant Value: 280 (0x00000118)

DENSITY_360

Added in API level 23
int DENSITY_360

Intermediate density for screens that sit somewhere between DENSITY_XHIGH (320 dpi) and DENSITY_XXHIGH (480 dpi). This is not a density that applications should target, instead relying on the system to scale their DENSITY_XXHIGH assets for them.

Constant Value: 360 (0x00000168)

DENSITY_400

Added in API level 19
int DENSITY_400

Intermediate density for screens that sit somewhere between DENSITY_XHIGH (320 dpi) and DENSITY_XXHIGH (480 dpi). This is not a density that applications should target, instead relying on the system to scale their DENSITY_XXHIGH assets for them.

Constant Value: 400 (0x00000190)

DENSITY_420

Added in API level 23
int DENSITY_420

Intermediate density for screens that sit somewhere between DENSITY_XHIGH (320 dpi) and DENSITY_XXHIGH (480 dpi). This is not a density that applications should target, instead relying on the system to scale their DENSITY_XXHIGH assets for them.

Constant Value: 420 (0x000001a4)

DENSITY_560

Added in API level 21
int DENSITY_560

Intermediate density for screens that sit somewhere between DENSITY_XXHIGH (480 dpi) and DENSITY_XXXHIGH (640 dpi). This is not a density that applications should target, instead relying on the system to scale their DENSITY_XXXHIGH assets for them.

Constant Value: 560 (0x00000230)

DENSITY_DEFAULT

Added in API level 4
int DENSITY_DEFAULT

The reference density used throughout the system.

Constant Value: 160 (0x000000a0)

DENSITY_HIGH

Added in API level 4
int DENSITY_HIGH

Standard quantized DPI for high-density screens.

Constant Value: 240 (0x000000f0)

DENSITY_LOW

Added in API level 4
int DENSITY_LOW

Standard quantized DPI for low-density screens.

Constant Value: 120 (0x00000078)

DENSITY_MEDIUM

Added in API level 4
int DENSITY_MEDIUM

Standard quantized DPI for medium-density screens.

Constant Value: 160 (0x000000a0)

DENSITY_TV

Added in API level 13
int DENSITY_TV

This is a secondary density, added for some common screen configurations. It is recommended that applications not generally target this as a first class density -- that is, don't supply specific graphics for this density, instead allow the platform to scale from other densities (typically DENSITY_HIGH) as appropriate. In most cases (such as using bitmaps in Drawable) the platform can perform this scaling at load time, so the only cost is some slight startup runtime overhead.

This density was original introduced to correspond with a 720p TV screen: the density for 1080p televisions is DENSITY_XHIGH, and the value here provides the same UI size for a TV running at 720p. It has also found use in 7" tablets, when these devices have 1280x720 displays.

Constant Value: 213 (0x000000d5)

DENSITY_XHIGH

Added in API level 9
int DENSITY_XHIGH

Standard quantized DPI for extra-high-density screens.

Constant Value: 320 (0x00000140)

DENSITY_XXHIGH

Added in API level 16
int DENSITY_XXHIGH

Standard quantized DPI for extra-extra-high-density screens.

Constant Value: 480 (0x000001e0)

DENSITY_XXXHIGH

Added in API level 18
int DENSITY_XXXHIGH

Standard quantized DPI for extra-extra-extra-high-density screens. Applications should not generally worry about this density; relying on XHIGH graphics being scaled up to it should be sufficient for almost all cases. A typical use of this density would be 4K television screens -- 3840x2160, which is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.

Constant Value: 640 (0x00000280)

Fields

density

Added in API level 1
float density

The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

See also:

densityDpi

Added in API level 4
int densityDpi

The screen density expressed as dots-per-inch. May be either DENSITY_LOW, DENSITY_MEDIUM, or DENSITY_HIGH.

heightPixels

Added in API level 1
int heightPixels

The absolute height of the display in pixels.

scaledDensity

Added in API level 1
float scaledDensity

A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size.

widthPixels

Added in API level 1
int widthPixels

The absolute width of the display in pixels.

xdpi

Added in API level 1
float xdpi

The exact physical pixels per inch of the screen in the X dimension.

ydpi

Added in API level 1
float ydpi

The exact physical pixels per inch of the screen in the Y dimension.

Public constructors

DisplayMetrics

Added in API level 1
DisplayMetrics ()

Public methods

equals

Added in API level 1
boolean equals (Object o)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

The general contract for the equals and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

Parameters
o Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

equals

Added in API level 17
boolean equals (DisplayMetrics other)

Returns true if these display metrics equal the other display metrics.

Parameters
other DisplayMetrics: The display metrics with which to compare.
Returns
boolean True if the display metrics are equal.

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.

setTo

Added in API level 1
void setTo (DisplayMetrics o)

Parameters
o DisplayMetrics

setToDefaults

Added in API level 1
void setToDefaults ()

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
String a printable representation of this object.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.