What is the difference between px, dp, dip and sp?

19

What's the difference between px , dp , dip and sp on Android?

    
asked by anonymous 13.12.2013 / 14:01

2 answers

22
  • px :

DEFINITION: Corresponding to the number of pixels on the screen USE: Avoid using px for everything, only in very specific cases it is recommended.

  • sp :

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!

  • dip or dp :

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

  • in : (inches)

DEFINITION: Based on physical screen size

  • mm : (mm)

DEFINITION: Based on physical screen size

  • pt : (points)

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

    
13.12.2013 / 14:03
3
  • px is pixel .
  • sp is scale-independent pixels (scale-independent pixels).
  • dip is density-independent pixels (density-independent pixels).

Wheretouse:

  • sp:forsettingfonts;
  • dipforothersizedefinitions.

    dip==dp

As Android API Guides :

  • dp : It is an abstract unit that is based on the physical density of the screen. These units are relative to% w / d (dots per inch) of the screen, where w / w% is approximately equal to w / w%. When run on a higher density screen, the number of pixels used to draw 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.
  • sp : This is like the unit of 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.
  • px : Matches the actual pixels of the screen. This unit of measurement is not recommended because the actual representation may vary between devices; each of the devices may have a number other than pixels per inch and may have more or less total pixels available on the screen.

In addition to the ones described in the question, there are still other units of measure in Android:

  • pt : Dots - 1/72 of an inch based on physical screen size.
  • mm : Millimeters - Based on the physical size of the screen.
  • in : Inches - Based on the physical size of the screen.

The following image helps describe the relationship between Android units of measure:

  

Responsebasedandtranslated from SOen ;

    
30.03.2015 / 16:35