Screen size in Android is grouped into categories ldpi
, mdpi
, hdpi
, xhdpi
, xxhdpi
and xxxhdpi
. Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch (dpi
).
PX(Pixels):
px
is meant for absolute pixels. This is used if you want to give in terms of absolute pixels for width or height. Not recommended.DP/DIP(Density pixels / Density independent pixels):
dip == dp
. In earlier Android versions dip was used and later changed to dp
. This is alternative of px
.
Generally we never use px
because it is absolute value. If you use px
to set width or height, and if that application is being downloaded into different screen sized devices, then that view will not stretch as per the screen original size.
dp
is highly recommended to use in place of px
. Use dp
if you want to mention width and height to grow & shrink dynamically based on screen sizes.
if we give dp/dip
, android will automatically calculate the pixel size on the basis of 160 pixel sized screen.
SP(Scale independent pixels):
scaled based on user’s font size preference. Fonts should use sp
.
when mentioning the font sizes to fit for various screen sizes, use sp
. This is similar to dp
.Use sp
especially for font sizes to grow & shrink dynamically based on screen sizes
Android Documentation says:
when specifying dimensions, always use either
dp
orsp
units. Adp
is a density-independent pixel that corresponds to the physical size of a pixel at 160dpi
. Ansp
is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size