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.
dp = px / (dpi / 160)
320dp = 480px / (240dpi / 160)
When supporting different screen sizes (densities) in Android often the focus is on creating different layouts for every possible screen. I.E.
- ldpi
- mdpi
- hdpi
- xhdpi
- xxhdpi
- xxxhdpi
There are some things which are crucial, if you want to support different screen sizes:
- Use different drawables for every screen density bucket (
drawables-hdpi,drawables-xhdpi, etc) - Use
dpinstead ofpxas unit for size. - Avoid using absolute sizes, use margins and let Android scale it accordingly.

Comments