Explaining the basics of dp (density-independent pixel) — a measurement unit in Android that ensures the same physical size of UI elements on screens with different pixel densities. One dp always corresponds to 1/160 inch on the screen, regardless of whether the device has high or low resolution. According to Google Material Design (2025), all dimensions in Android applications should be specified in dp, not px, otherwise the interface will look different on different devices.
Key Takeaways
dp (density-independent pixel) is a virtual unit of measurement in Android, introduced to unify UI element sizes on devices with different pixel densities. One dp is approximately equal to one physical pixel on a screen with 160 dpi density (mdpi). On a screen with 320 dpi density (xhdpi), 1 dp equals 2 physical pixels. Thus, the physical size of an element on the screen remains the same — about 1/160 inch. According to Android Developers (2025), dp is the only unit of measurement that should be used for layout, padding, margin and view width/height dimensions.
If you set a button width to 160 px, on an mdpi screen it will be 1 inch wide, but on an xxxhdpi screen (640 dpi) — only 0.25 inches. dp solves this problem: 160 dp on any screen will equal 1 inch. Android automatically converts dp to px via DisplayMetrics.density during rendering. According to the Android Developer Blog (2025), before the introduction of dp (Android 1.0), developers manually calculated px offsets, which led to constant bugs when porting.
dp, px and sp are three units of measurement in Android, each with its own purpose. px is a physical screen pixel that depends on density. dp is a density-independent pixel for element dimensions. sp is a scale-independent pixel for text that additionally takes into account system font size settings. According to Material Design (2025), the difference between dp and sp is a key accessibility point: users with poor eyesight increase font size, and sp values adjust automatically, while dp does not.
| Unit | Full name | Accounts for density | Accounts for font settings | Usage |
|---|---|---|---|---|
| px | Pixel | No | No | Not recommended |
| dp | Density-independent Pixel | Yes | No | Layout, padding, margins |
| sp | Scale-independent Pixel | Yes | Yes | Text size |
The conversion formula for dp to px: px = dp × (dpi / 160). For reverse conversion: dp = px / (dpi / 160). density is the ratio dpi / 160, which Android calculates based on DisplayMetrics. On mdpi density = 1.0, on hdpi = 1.5, on xhdpi = 2.0, on xxhdpi = 3.0, on xxxhdpi = 4.0. Thus, 48 dp in pixels: on mdpi — 48 px, on xxhdpi — 144 px. According to Google I/O 2024, this formula can be used for approximate conversion of iOS pt (points) to dp — 1 pt ≈ 1 dp.
| Bucket | Density | dpi value | 48 dp in px | 16 dp in px |
|---|---|---|---|---|
| mdpi | 1.0 | 160 | 48 | 16 |
| hdpi | 1.5 | 240 | 72 | 24 |
| xhdpi | 2.0 | 320 | 96 | 32 |
| xxhdpi | 3.0 | 480 | 144 | 48 |
| xxxhdpi | 4.0 | 640 | 192 | 64 |
dimens.xml is an Android resource file in res/values/, intended for storing all dimensions in dp (and sp for text). Centralizing dimensions in dimens.xml simplifies maintenance: changing one value in dimens.xml updates the size across all application screens. You can create qualified dimens.xml files for different densities (values-hdpi, values-xhdpi). According to Android Jetpack Docs (2025), Google recommends storing margins, padding, element widths (constants), avatar sizes, and toolbar height in dimens.xml.
<resources>
<dimen name="margin_small">8dp</dimen>
<dimen name="margin_medium">16dp</dimen>
<dimen name="margin_large">24dp</dimen>
<dimen name="avatar_size">48dp</dimen>
<dimen name="toolbar_height">56dp</dimen>
<dimen name="text_size_body">14sp</dimen>
<dimen name="icon_size_small">24dp</dimen>
</resources>
In this file, each dimen has a name and a value. Usage in XML markup: @dimen/avatar_size. According to Material Design Guidelines (2025), the basic grid step in Android is 8 dp (8-point grid). All margins, element sizes, and distances should be multiples of 8 dp: 8, 16, 24, 32, 48, 56, 64, 72 dp. For text, the step is 4 sp.
In XML markup, dp is used directly: android:layout_width="48dp". In programmatic code (Kotlin/Java), you need to convert dp to px using TypedResource or extension functions. TypedResource provides ready-made methods for working with dp at runtime: getDimensionPixelSize() returns an integer px value. In Jetpack Compose, dp is the standard unit of measurement in modifiers and layout containers.
// Converting dp to px programmatically
fun Context.dpToPx(dp: Int): Int {
return (dp * resources
.displayMetrics
.density).toInt()
}
// Usage in Activity
val paddingPx = dpToPx(16)
button.setPadding(
paddingPx,
paddingPx,
paddingPx,
paddingPx,
)
// Jetpack Compose: dp as a built-in unit
Modifier
.padding(16.dp)
.size(48.dp)
In this example, dpToPx() is an extension function for Context, converting dp to px using displayMetrics.density. In Jetpack Compose, dp is built into the type system and does not require manual conversion — the compiler automatically translates dp to px at the rendering stage.
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:padding="@dimen/margin_medium"
android:textSize="@dimen/text_size_body"
android:text="@string/button_label"
android:layout_marginTop="16dp"
/>
In XML, dp is specified as a suffix to the number — margin_top="16dp". For text, sp is used — textSize="@dimen/text_size_body". The @dimen reference allows centralized value management. Using match_parent for width and 48dp for height is a standard Material Design pattern for buttons.
According to an analysis of Android applications from Google Play Console (2025), the five most common mistakes are: using px instead of dp in XML markup, missing dimens.xml (values written inline), incorrect dp to px conversion in code (using integer division), ignoring sp for text, and not accounting for system font size settings via sp. Integer division is the most dangerous mistake: in Kotlin val px = dp * density — if dp and density are Int, the result will also be Int, and precision is lost.
Frequently Asked Questions
dp in Android and pt (points) in iOS are equivalent units of measurement. 1 dp on Android is approximately equal to 1 pt on iOS. Both concepts describe density-independent points: on older devices with low density 1 dp/pt = 1 px, on modern ones — 2-4 px. The difference is in the base value: Android uses 160 dpi as the base density, iOS uses 163 dpi, resulting in a margin of error of less than 2%.
In Jetpack Compose, no conversion is required — dp is built into the type system. All modifiers and layouts accept Dp, not px. If you need to get px from dp, use density: val px = with(LocalDensity.current) { 16.dp.toPx() }. LocalDensity provides the conversion factor that takes into account the screen density of the device.
dp is an Android term. In iOS, the equivalent is pt (points). Swift automatically works with points that are independent of screen density. In UIKit and SwiftUI, all dimensions are specified in points, and the system converts them to px using the screen's scale factor (UIScreen.main.scale). Same principle, different name.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also