Last Updated: September 27, 2021
· itseranga

Android terms(dip/dp, px, dpi, display matrix)

Display size

Actual physical size of the screen.

Screen resolution

Screen resolution is total no of pixel in screen. Following program will extract the screen resolution of the device. It will print screen width and height. Those values are in pixel.

* Get screen resolution/width and height of the device
* Return as a 'Point'(This point can use to get width and height of the device)
* @param context need to get window manager
* @return resolution point
public static Point getScreenResolution(Context context) {
    // get window managers
    WindowManager manager =  (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    Point point = new Point();

     // get width and height

     return point;

Screen density

Is a ratio between display size and screen resolution. Density defines no of pixels in a single unit of a display. Measures with DPI value. DPI - Dots Per Inch. There are four types of screen densities in android

  • Low - 120 dpi
  • Medium - 160 dpi
  • High - 240 dpi
  • Extra high - 320 dpi

Low density screens have low amount of pixels in single unit, and high density screens have high amount of pixels in single unit(Normally single uni is an Inch). Following program will extract the density(dpi value) of the screen.

float density = context.getResources().getDisplayMetrics().density;

dip/dp vs px

  • dip/dp - Density Independent Pixel. Use when define layout attributes such as height, width(example - android:layout_width="34dp"). The density independent pixel is equivalent to one physical pixel on a 160 dpi screen. So in 240 dip screen 1dip = 1.5pixel. 320 dip screen 1dip = 2pixel.

  • px - Corresponds to actual pixels on the screen.

Convert dp to px

When setting width/height of a layout programatically, we have to set it eith pixel. So dp value need to convert to pixel and set to the layout. Following is the equation to convert dp to px

px = dp * (dpi / 160). Following program converts dp to px

 * Convert dp/dpi to pixel values
 * @param context need to get display metrics
 * @param dp DP value
 * @return pixel value
public static int dpToPx(Context context, int dp) {
    float density = context.getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);

More info -

Display matrix


  • Screen resolution gives height/width of whole screen(includes the height of the status bar)

  • Action bar height is the height of the action bar. Following program gives height of action bar

// will returns height of the action bar in pixel
  • Content view size is the size of root view of your layout. Following programs will gives the content view attributes
 * Get content view of an activity(this is the root view of any layout)
 * This method should call after activity view created(onWindowFocusChanged)
 * @param context app context
 * @return root view
public static View getContentView(Context context) {
    View contentView = ((Activity)context).getWindow().findViewById(Window.ID_ANDROID_CONTENT);

    // content view height/widht

    return contentView;
  • You can't usually gain access to a true value of that view's size until layout is complete. So if we call above method in onCreate() or onResume() it won't give the actual sizes(Will return 0). In order to get the actual values you have to call above method in onWindowFocusChanged() which will be called at the point when your Activity is just about visible to the user.

Status bar height

According to the above layout, status bar height will be (screen resolution height - root view height).