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..
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.
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:
Just clone the project and open it with Android Studio.
Any ideas to improve or customize the tool? Send your Pull Request : ) !