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

In android there are different pixel measurement units. What is the difference between the following?

  • px
  • dp
  • dip
  • sp
 25
Author: Rafael Bautista Martinez, 2015-12-01

2 answers

According to the specifications of Google units and measures :

Pixel density

Is the number of pixels that fits in an inch. High-density displays have more pixels per inch than low-density ones. This results in some graphic elements (such as buttons) looking larger on a low-density screen and smaller on a high-density one.

En Android:

  • px is a pixel.
  • dip or dp are density-independent pixels.
  • sp are scaleable pixels.

Pixels of independent density (dp or dip)

Are flexible units that scale to uniform dimensions on any display. When developing applications for Android, use dp to display the elements evenly on screens with different densities.

A dp is equal to one pixel on a 160 density screen. To calculate dp:

dp = (width in pixels * 160) / screen density

Independent scale pixels (sp)

Have the same function as dp, but are used for fonts. The default value of a sp is the same as the default value of a dp.

The main difference between a sp and a dp is that the sp preserves the user's settings with respect to the source. Users who have settings to enlarge the text will see that the size of the text matches their preferences.

As a conclusion:

  • dp and dip is the same and is used for view objects (although it can also be used for text, not recommended).
  • sp is used only for text.
 29
Author: raukodraug, 2015-12-01 20:40:47

The 4 are units related to the pixels:

Px : I could mention that it is only a "Pixel", but that it is a Pixel, it is the smallest homogeneous unit in color that is part of a digital image.

Dp : the density of independent pixels - a summary of the unit that is based on the physical density of the screen. Be units are relative to a 160 dpi display, so a dp is a pixel in a 160 dpi display extension. The relationship of dp-a-pixel will change with the screen density, but not necessarily in direct proportion.

Dip : it is the same as dp.

Sp : independent of the Pixel scale, this is like the dp of the unit, but it is also scaled by the user font size preference. It is recommended to use this unit when specifying the font sizes, so it will be adjusted so that both the density and user preference.

Review:

Compatibility between different densities.

Support for different screen sizes

 4
Author: Jorgesys, 2019-03-02 19:53:56