Converting pixels to dp
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On Android, raw pixels are not a reliable unit for defining UI size because devices have different screen densities. A view that is 100 pixels wide on one device can look physically much smaller on another. That is why Android uses dp, short for density-independent pixels, as the standard layout unit.
What dp Represents
dp is a virtual unit that scales with screen density. The baseline density is 160 dots per inch. At that density, 1 dp maps to 1 px. At higher densities, more pixels are used to represent the same physical size.
In practice, Android gives you a density scale factor. The simplest conversion formula is:
- '
dp = px / density' - '
px = dp * density'
Here, density is typically something like 1.0, 1.5, 2.0, 3.0, or 4.0 depending on the device class.
Converting Pixels to dp in Code
The recommended way is to use DisplayMetrics from the current context.
Usage:
This returns an integer value suitable for many UI calculations.
Converting dp Back to Pixels
Many UI APIs ultimately need pixel values at runtime. For example, when setting padding or drawing dimensions programmatically, convert from dp to px.
This is the direction you will probably use more often in application code, because layouts are usually designed in dp and converted to pixels only when needed by a low-level API.
Why Layout XML Should Usually Stay in dp
If you are defining margins, widths, heights, or padding in XML, use dp directly instead of converting manually.
This lets Android handle density scaling automatically. Manual conversion is mainly for runtime code paths.
A Concrete Example
Suppose a device has a density of 2.0, meaning it uses twice as many pixels as the baseline for the same physical size.
- '
48 pxbecomes24 dp' - '
24 dpbecomes48 px'
That explains why copying pixel values from design tools directly into Android code often leads to inconsistent sizing. Without density-aware conversion, the UI becomes device-dependent.
dp Versus sp
A frequent source of confusion is mixing dp and sp.
- use
dpfor layout size and spacing - use
spfor text size
sp behaves like dp but also respects the user's font scaling preference. If you use dp for text, accessibility suffers.
Utility Extensions for Cleaner Code
In Kotlin projects, extension properties can make these conversions less repetitive.
Then you can write:
This is convenient, but use application or view resources when possible if you need the exact current configuration rather than the system default.
Common Pitfalls
The biggest mistake is designing in pixels and carrying those pixel measurements directly into Android layouts. That makes the UI inconsistent across densities.
Another issue is doing integer division too early. If you divide pixel values before converting to floating point, you can lose precision.
Do not use dp for text size. Use sp.
Also be careful with Resources.getSystem() in libraries or themed contexts. It can be fine for utilities, but if you already have a Context, using that context's display metrics is usually clearer and safer.
Summary
- Android uses
dpso layouts stay physically consistent across screen densities. - Convert pixels to
dpwithpx / density. - Convert
dpto pixels withdp * density. - Use
dpin XML layouts whenever possible instead of converting manually. - Use
spfor text, notdp. - Prefer context-aware display metrics when converting values in runtime code.

