px && dp

14 Apr 2023 . android .

px

Pixels - corresponds to actual pixels on the screen. eg: 320px*480px

in

Inches - based on the physical size of the screen.

dpi

Dots Per Inch - the number of pixels contained in each inch of screen.

  • For example, we have a device 2 inches wide and 3 inches high:
    • When the resolution is “320*480”, the dpi is 160.
    • When the resolution is “640*960”, the dpi is 320.

    The higher the dpi, the better the screen will be displayed.

dp/dip

Density independent Pixels - an abstract unit that is based on the physical density of the screen.

  • $px = dp * (dpi/160)$
    • For device with a resolution of 320*480 (i.e. 160 dpi), 160dp = 160px, the button accounts for half of the screen width.
    • For device with a resolution of 640*960 (i.e. 320 dpi), 160dp = 320px, the button accounts for half of the screen width.

sp

Scaleable Pixels - this is like the dp unit, but it is also scaled by the user’s font size preference.