- Why was dp created when there were already other density independent units such as cm?
- Is the dp to px conversion by the Android system based on buckets rather than the exact device's pixel density? Wouldn't this result in a discrepancy in how much physical space elements measure on different device screens?

- 57
- 1
- 5
-
Welcome to SO. Best way to find your answers is implementing them by your own in demo apps. Such questions are not meant to be posted on SO. – prashant0205 Jun 24 '18 at 15:00
2 Answers
Because cm are too big. You'd have to work with floats, which means you'd need to round all the time. They wanted something smaller. Also, the first devices were 160 dpi, do 1dp=1px which was convenient at the time (now very few devices ship at mdpi so this advantage is gone). It also just so happens to match the dpi of the iphone, rounded to the nearest 10 (iphone was 163 dpi), so measurements were what mobile devs were used to making easy conversions.
Its based on the physical dimensions. There can be some discrepancy, but in practice there isn't. If you have an image that is a little too small, the amount of distortion you get from stretching the tiny amount is negligible. The other major use of dp is in padding, and there the slight rounding errors are unnoticable as long as you're consistent on a single device.

- 72,056
- 11
- 123
- 141

- 90,003
- 9
- 87
- 127
- As said by @Gabe Sechan, cm are too big (even mm). Contrary to cm, conversion to pixels is based on density category rather than exact density of the screen, which avoids rounding issues you'd have with cm to px (factor of 1.5x, 2x, 3x, etc rounds much better than an "arbitrary" ppi).
- Yes. Conversion of dp to px is based on the density bucket the screen belongs to (mdpi, hdpi, xhdpi, etc) rather than the exact physical ppi. Bitmap graphics of an app are categorized by buckets, so for measurement consistency the dp is converted based on the density bucket rather than the real ppi. The drawback is that unlike a unit such as cm, the measurements are not guaranteed to be the same physical size on every screen (example of such inconsistency).
References:
Why aren't standard physical length units like mm used for touch UI layout?
Why “dp-to-pixel” ratio changes with the screen density, but not necessarily in direct proportion

- 57
- 1
- 5