Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

DIP, dp, sp and PX

• Screen size in Android is grouped into categories small, medium,


large, extra large, double-extra and triple-extra.
• Screen density is the amount of pixels within an area (like inch) of the
screen. Generally it is measured in dots-per-inch (dpi). Screen density
is grouped as low, medium, high and extra high.
• Resolution is the total number of pixels in the screen.
Relation between pixels and density independent
pixels
• Taking an example of three devices of the same physical size but different
resolution.

If we define Button’s height and width in pixel, this is going to happen in different device resolution. Button covers 2 pixels
horizontally and 2 pixels vertically but the pixel density(resolution) is different which makes our button size small.
We will use dp as a measurement unit.
• dp: Density Independent Pixel, it varies based on screen density . In 160 dpi screen, 1
dp = 1 pixel. Except for font size, use dp always.
• dip: dip == dp. In earlier Android versions dip was used and later changed to dp.
• sp: Scale Independent Pixel, scaled based on user’s font size preference. Fonts should
use sp.
• px: our usual standard pixel which maps to the screen pixel.
• in: inches, with respect to the physical screen size.
• mm: millimeters, with respect to the physical screen size.
• pt: 1/72 of an inch, with respect to the physical screen size.
Always use dp and sp only. sp for font sizes and dp for everything else.
It will make UI compatible for Android devices with different densities.
Formula for Conversion between Units

px = dp * (dpi / 160)

In Android, we have a baseline density of 160 dots-per-inch(dpi). So,


for a 160 dpi screen, we have 1 pixel = 1 dp and 320 dpi screen, we
have 2 pixels = 1 dp which is 2x.
• The scaling occurs based on bucket size of 120(ldpi),
160(mdpi), 240(hdpi), 320(xhdpi), 480(xxhdpi) and
640(xxxhdpi).
• A 150px X 150px image will occupy,
1. 150 dp X 150 dp screen space in mdpi
2. 100 dp X 100 dp screen space in hdpi
3. 75 dp X 75 dp screen space in xhdpi

You might also like