I was reading dp, dip, px, sp measurements, but I still have some questions about dp/dpi vs ppi vs px vs inch. I am not able to compare them... is an inch the largest?
They say 160 dpi means 160 pixels per one inch. Does that mean 1 inch contains 160 pixels?
They also say 1 pixel on a 160 dpi screen = 1 dp. Does that mean 1 pixel and 1 dp are equal?
And lastly, why should we use dp instead of px? I understand that it is ideal, but why?
You should (almost) always use flexible sizing units, like dp
, which is Density-Independent Pixels, because 300px
on one device is not necessarily the same amount of screen real estate as 300px
on another. The biggest practical implication is that your layout would look significantly different on devices with a different density than the one your design targeted.
dp
or dip
means Density-independent Pixelsdpi
or ppi
means Dots (or Pixels) Per Inchinch
is a physical measurement connected to actual screen sizepx
means Pixels — a pixel fills an arbitrary amount of screen area depending on density.For example, on a 160dpi
screen, 1dp == 1px == 1/160in
, but on a 240dpi
screen, 1dp == 1.5px
. So no, 1dp != 1px
. There is exactly one case when 1dp == 1px
, and that's on a 160dpi
screen. Physical measurement units like inches should never be part of your design—that is, unless you're making a ruler.
A simple formula for determining how many pixels 1dp
works out to is px = dp * (dpi / 160)
.