to top
Android APIs
public class

DisplayMetrics

extends Object
java.lang.Object
   ↳ android.util.DisplayMetrics

Class Overview

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_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.
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
void setTo(DisplayMetrics o)
void setToDefaults()
String toString()
Returns a string containing a concise, human-readable description of this object.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int DENSITY_DEFAULT

Since: API Level 4

The reference density used throughout the system.

Constant Value: 160 (0x000000a0)

public static final int DENSITY_HIGH

Since: API Level 4

Standard quantized DPI for high-density screens.

Constant Value: 240 (0x000000f0)

public static final int DENSITY_LOW

Since: API Level 4

Standard quantized DPI for low-density screens.

Constant Value: 120 (0x00000078)

public static final int DENSITY_MEDIUM

Since: API Level 4

Standard quantized DPI for medium-density screens.

Constant Value: 160 (0x000000a0)

public static final int DENSITY_TV

Since: API Level 13

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)

public static final int DENSITY_XHIGH

Since: API Level 9

Standard quantized DPI for extra-high-density screens.

Constant Value: 320 (0x00000140)

public static final int DENSITY_XXHIGH

Since: API Level 16

Standard quantized DPI for 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.

Constant Value: 480 (0x000001e0)

Fields

public float density

Since: API Level 1

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

public int densityDpi

Since: API Level 4

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

public int heightPixels

Since: API Level 1

The absolute height of the display in pixels.

public float scaledDensity

Since: API Level 1

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.

public int widthPixels

Since: API Level 1

The absolute width of the display in pixels.

public float xdpi

Since: API Level 1

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

public float ydpi

Since: API Level 1

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

Public Constructors

public DisplayMetrics ()

Since: API Level 1

Public Methods

public void setTo (DisplayMetrics o)

Since: API Level 1

public void setToDefaults ()

Since: API Level 1

public String toString ()

Since: API Level 1

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
  • a printable representation of this object.