Thinking dp as centimeters (or inches)

The title may be a little misleading, because in fact dp can’t be thought as a unit of (real world) measurement, it is a unit of measurement!
Following the definition of dp (other for designers):

Density-independent pixel (dp)

A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a “medium” density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application’s UI, to ensure proper display of your UI on screens with different densities.

We get something, dp is a pixel in a 160dpi screen.. ok.. for some android students or early programmers/it people that definition can still be confusing (even though is the exact definition). We also have the conversion formula, from dp to px, which is the one the system uses for giving dimension to objects to be drawn (dimension measured in pixels).

What about the point of this article? Well simple (the -est) math or logic..

160 px = 160 dp \\ \text{in a 160dpi screen} \\ \\ \text{In each inch there are 160 dots a.k.a. pixels} \\ \\ \therefore 160dp = 1 in \\ \therefore 160dp = 2.54 cm

So the dpi term there was connecting those virtual units (considering pixels virtual too) to real world units (inches in that case). Google also relates dp to real world units when talking about touch target size but honestly I cannot realize what calculations result in 48dp being 9mm (even the button diagonal would be more than 10mm) – NOTE: That would make all this article a nonsense, if you get the point, share your thoughts here solving one of the mini-misteries of mankind.

So that’s it, for example 1cm is almost 62.99dp and 400dp is 6.4cm.

Do you want to just type your dp and get how much is it? Try this mini js app.

Android Grid

If you were in need of displaying a grid with screen measurements in android, this project does the job where the real size of the screen can also be seen:

Screen dimensions grid draws over all the screen (except the notifications bar)

Just clone the project and open it with Android Studio.

Any ideas to improve or customize the tool? Send your Pull Request : ) !

Loading Disqus Comments ...
Loading Facebook Comments ...