What is the difference between px, dip, dp, and sp?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On Android, px, dp, dip, and sp are all measurement units, but they are not interchangeable. The short version is simple: px is a raw physical screen pixel, dp and dip are density-independent layout units, and sp is a text unit that also respects the user’s font scaling preference. Using the right one is what keeps an interface readable across many devices and accessibility settings.
px: Raw Pixels
px means actual screen pixels.
If you set something to 100px, you are asking for 100 physical pixels on that device’s display. Because Android devices have very different pixel densities, the same pixel count can look physically small on one phone and much larger on another.
That is why px is rarely the right choice for normal Android layout code.
Example:
This may be technically valid, but it is usually a bad responsive-design choice.
dp and dip: Same Concept, Same Unit
dp means density-independent pixels. dip is simply the older name for the same unit. In modern Android code, use dp.
The point of dp is to keep visual size roughly consistent across screen densities. Android treats 160 dpi as the baseline where:
At higher densities, the system scales automatically.
So a 16dp margin should feel like roughly the same physical spacing across devices even though the actual pixel count differs.
Example:
This is the normal unit for layout dimensions, padding, margins, and view sizes.
sp: Scale-Independent Pixels for Text
sp is like dp, but it also respects the user’s font size preference.
That is the crucial difference.
If a user increases system text size for accessibility, sp-based text grows with that preference. dp-based text does not respond correctly in the same way.
Example:
This is why sp is the standard unit for text sizes.
A Practical Rule of Thumb
Use these defaults:
- '
dpfor layout size, margins, padding, and most view dimensions' - '
spfor text size' - '
pxonly when you truly need raw pixel math' - '
diponly when reading old code or old documentation, not for new style'
That simple rule covers most Android UI work.
Why sp Should Not Usually Be Replaced with dp
Some developers use dp for text because they want text to stay visually fixed. That usually creates accessibility problems.
If a user has requested larger system text, an sp value honors that choice. A dp value ignores it.
For most app UIs, respecting text scaling is the correct behavior.
That is one of the reasons Android distinguishes sp from dp at all.
Converting Between Units in Code
Sometimes you need to convert programmatically.
In actual Android code, you usually get density values from Resources.getDisplayMetrics().
Even when doing conversions manually, the semantic distinction still matters: sp uses scaled density, not just display density.
Compose and Modern UI Toolkits
The same ideas still apply in Jetpack Compose, even though the syntax is different.
and:
So the underlying concepts are not tied only to old XML layouts.
Common Pitfalls
The biggest pitfall is using px for normal layout sizes and then wondering why the UI looks inconsistent across devices.
Another issue is using dp for text and accidentally ignoring user font scaling preferences.
Developers also sometimes think dip and dp are different units. They are not.
Finally, manual conversion code can go wrong if you mix up density and scaledDensity.
Summary
- '
pxis a raw physical pixel count.' - '
dpanddipmean the same density-independent layout unit.' - '
spis for text and respects user font scaling.' - Use
dpfor layout measurements andspfor text sizes. - Avoid
pxunless you really need exact pixel-level behavior.

