What is the difference between these units of measurement on Android?
px
dip
dp
sp
Answer 1, authority 100%
According to documentation :
-
px
Pixel – corresponds to one pixel on the screen. It is not recommended to use, as the elements will look different on different devices (the physical size of one pixel is different on each device). -
in
Inch – corresponds to the actual physical size of the elements on the screen (1 inch ≈ 2.54 & nbsp; cm) -
mm
Millimeter – also corresponds to the real size (1 mm = 0.01 & nbsp; cm) -
pt
Dot – 1/72 inch, real size (1 dot ≈ & nbsp; 3.5 mm) -
dp or dip
Density-independent Pixel is a relative unit of measurement based on the density of a device’s screen. Corresponds to about one pixel on a 160 dots per inch (dpi) device. The dp / px ratio changes according to the screen density, but not in strict proportion: dp / dx depends on the screen category, not the exact density value. It is recommended to use px instead of usual for correct display of elements on devices with different screen densities. You can write bothdip
anddp
in markup, the compiler treats both options the same, butdp
is preferred. -
sp
Scale-independent Pixel – a pixel independent of scaling – like dp, it depends on the screen density, but the result will be scaled according to the user’s font size settings. Recommended for setting font sizes so that the text adapts to both screen density and user-defined settings
From Understanding Density Independence In Android :
+ --------- + -------------- + --------------- + ------------ + ------------- +
| Unit | Name | In 1 inch | Depends on | Identical |
| | | | density? | physical size |
+ --------- + -------------- + --------------- + -------- ---- + ------------- +
| px | Pixel | - | Yes | No |
+ --------- + -------------- + --------------- + -------- ---- + ------------- +
| in | Inch | 1 | No | Yes |
+ --------- + -------------- + --------------- + -------- ---- + ------------- +
| mm | Millimeter | 25.4 | No | Yes |
+ --------- + -------------- + --------------- + -------- ---- + ------------- +
| pt | Point | 72 | No | Yes |
+ --------- + -------------- + --------------- + -------- ---- + ------------- +
| dp | Pixel, | ~ 160 | No | No |
| | independent | | | |
| | on the density | | | |
+ --------- + -------------- + --------------- + -------- ---- + ------------- +
| sp | Pixel, | ~ 160 | No | No |
| | independent | | | |
| | from the scale- | | | |
| | rationing | | | |
+ --------- + -------------- + --------------- + -------- ---- + ------------- +
Visual: Google Design Documentation .