What's the difference between px
, dp
, dip
and sp
on Android?
What's the difference between px
, dp
, dip
and sp
on Android?
DEFINITION: Corresponding to the number of pixels on the screen USE: Avoid using px for everything, only in very specific cases it is recommended.
DEFINITION: (Scale-independent Pixels) Same as dp, but also considers the font size that the user is using. It is recommended that you use this unit when specifying the size of a font so that it is automatically adjusted according to the preferences of the user's screen. USE: Always use sp for fonts!
Density-independent Pixels This unit is relative to the screen resolution. For example if the resolution of the screen is 160 dpi, it means that a dp represents 1 pixel in a total of 160. USE: I advise instead of using px always use dp.
We still have other units of measures used by Android
DEFINITION: Based on physical screen size
DEFINITION: Based on physical screen size
DEFINITION: 1/72 of an inch, based on the physical size of the screen
More about units of measurements and dimensions in Android here A super explanatory video created by @NetoMarin link
Wheretouse:
sp
:forsettingfonts;dip
forothersizedefinitions.
dip==dp
As Android API Guides :
160dpi
is sized by an appropriate screen factor. Likewise, when in a lower density screen, the number of pixels used for 1dp
is reduced. The ratio of% w / w% to pixel changes with screen density, but not necessarily in direct proportion. Using units 1px
(instead of pixel units) is a simple solution for creating views in your layout able to properly resize to different screen densities. In other words, it provides consistency for the actual sizes of your UI elements on different devices. 1dp
, but is also scaled by the font size to the user's preference. It is recommended to use this unit when specifying font sizes, so it will be adjusted for both screen density and user preference. In addition to the ones described in the question, there are still other units of measure in Android:
The following image helps describe the relationship between Android units of measure:
Responsebasedandtranslated from SOen ;