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.
If you want to know what's 56dp in xhdpi, that's 56dp in ldpi, mdpi, hdpi, xxhdpi, xxxhdpi as well - as the definition says, it's density-independent pixels.
When you work with a design, you want to know one thing:
What's the baseline scale for the design?
Some quick answers: It's usually 1x, 2x or in rare cases 3x
Android defines the following densities:
- ldpi (low) ~120dpi
- mdpi (medium) ~160dpi
- hdpi (high) ~240dpi
- xhdpi (extra-high) ~320dpi
- xxhdpi (extra-extra-high) ~480dpi
- xxxhdpi (extra-extra-extra-high) ~640dpi
There are some rare cases in-between, like tvdpi, but in 99% of the cases you can just simply ignore those.
When designers say 1x, they usually refer to mdpi, 2x (or retina) refers to xhdpi and 3x design is done in xxhdpi.
Once you know the baseline, you know how to translate the pixel values from the design document to dip (density-independent pixel).
The formula is quite simple, but if you doubt yourself, feel free to try one of the online converter tools:
The formula is as follows: dp = px / (dpi / 160) where dpi is the current screen density and px is the value in the base (1x) density.
Or the other way around.
Also, remember to read the Best Practices from the Android Developers page!
If you still end up using px in your dimensions file which are not recommended, please take a look at this answer to know how to create different values folder for different screens.