About pixels ↔ dp
Android measures screens in density-independent pixels (dp) so the same layout looks about the same size on phones with different pixel densities.
Formula
px = dp × dpi ÷ 160. At xhdpi (320 dpi) 16 dp is 32 px; at xxhdpi (480 dpi) it is 48 px. To go the other way, dp = px × 160 ÷ dpi.
Density buckets
- ldpi 120, mdpi 160 (the baseline), hdpi 240, xhdpi 320.
- xxhdpi 480 and xxxhdpi 640 for high-resolution screens.
Use dp for sizes, sp for text
Use dp for layout dimensions and sp for text so fonts respect the user's font size setting.
A worked example
A phone with a 1080 × 2400 pixel screen and a density of 420 dpi has a width of 1080 × 160 ÷ 420 ≈ 411 dp. A button designed as 48 dp tall is 48 × 420 ÷ 160 = 126 px on that phone, but 144 px on a 480 dpi (xxhdpi) one.
Tips
- Design in dp and let Android convert it, rather than hard-coding pixels.
- Supply image assets for several densities, or use vector drawables so one file fits all.
- Keep touch targets at least 48 dp so they are easy to tap.
