Skip to content
Related Articles

Related Articles

Improve Article

What is the Difference Between “px”, “dip”, “dp” and “sp” in Android?

  • Last Updated : 23 Jul, 2021
Geek Week

An XML-defined dimension value. A dimension is denoted by a number followed by a unit of measurement. For instance, 25px, 5in, 10dp and 10sp. When you use sp/dp, your Android applications will be compatible with a wide range of screen densities and resolutions.

  • PX: is an abbreviation for Pixels, which specifies the actual pixels on the screen.
  • SP: is an abbreviation for Scale independent pixels. It is the same as the dp unit, but it is additionally scaled according to the user’s font size selection.
  • DP: A virtual pixel unit used to communicate layout dimensions or location in a density-independent manner while creating UI layout. The density-independent pixel corresponds to one physical pixel on a 160 dpi screen, which is the system’s baseline density for a “medium” density screen. At runtime, the system handles any scaling of the dp units that is required based on the actual density of the screen in use in a transparent manner.

The terms DP and DIP refer to Density Independent Pixels, which are based on the physical density of the screen. 

  • SP: Similar to dp, but also scaled by the user’s font size selection. When choosing font sizes, it is recommended that you use this unit so that they are adjusted for both screen density and user choice.

1. Orientation of the Android Device

The orientation of the screen is seen by the user. This is either landscape or portrait, indicating that the screen’s aspect ratio is broad or tall. Be aware that various devices not only function in different orientations by default but that the orientation might change at runtime when the user spins the device.

2. The density of the screen

The number of pixels inside a physical region of a screen; sometimes abbreviated as dpi (dots per inch). A “low” density screen, for example, contains fewer pixels inside a given physical area than a “normal” or “high” density screen. Android categorizes all real screen densities into six generic densities for ease of use: low, medium, high, extra-high, extra-extra-high, and extra-extra-extra-high.

3. Resolution of the Android Device

A screen’s entire amount of physical pixels. When providing support for multiple screens, programs should not be concerned with resolution; instead, they should be concerned with screen size and density, as described by the generic size and density groups.



Difference Table

px (pixels) 

in (inches)

mm (millimeters) 

pt (points)

dp or dip (Density-independent Pixels)

Refers to the actual pixels on the screen.Depending on the actual size of the screen in inches.Determined by the actual size of the screen.Based on the actual size of the screen, 1/72 of an inch, assuming a screen with a resolution of 72dpiAn abstract unit based on on-screen physical density. These measurements are measured in relation to a 160 dpi screen.
The compiler only accepts pxThe compiler only accepts in (however this unit is not recommended because it may vary)The compiler takes mm, but not by default (takes sp)Not a familiar unit, used in old Android Programming, by Eclipse. Still, the compiler takes it.The Compiler accepts both “dip” and “dp,”
The actual physical present pixels of the screen is measured by its diagonal. Android combines all real screen sizes into four generic sizes for ease of use: small, standard, big, and extra-large.The actual screen estate which is available on the running device is accessible to the Android OS.The usual unit of measurement is millimeters, used like in everyday measurements.Similar to the dp unit, but scaled by the user’s font size selection. When choosing font sizes, it is suggested that you use this unit so that they are adjusted for both screen density and user choice.The dp-to-pixel ratio will alter with screen density, but not always in a straight proportion. However “dp” is more consistent with “sp.”
Can be used to determine the layouts and elements places on the layout.Can be used to scale the elements on the screen concerning the actual screen.Should not be used for setting and determining the layouts due to the variations possible. Can be used to design the layout just like px but is preferred less as compared to the other measurements. The “sp” here is only used for text; it should never be used for layout sizes.
px = dp * (dpi / 160)N/A (as the measurement is absolute)N/A (as the measurement is absolute)N/A (as the measurement is absolute)Density = sqrt((wp * wp) + (hp * hp)) / di
Represented in terms of x*y.
X is the horizontal axis, Y is the vertical.
Represented in absolute digits and integers within as suffixRepresented in absolute digits and integers within as mmRepresented as pts or pt depending on the version of Android Studio you are using.Represented as simply ‘dp’ and to use with text use ‘sp’.
NOT density-independentDensity IndependentDensity IndependentDensity IndependentDensity Independent

GeekTip: A dimension is a basic resource that is referred to by the value specified in the name property (not the name of the XML file). As a result, dimension resources may be combined with other basic resources in a single XML file, under a single resource > element.

Conclusion

Pixels per inch are higher on high-density screens than on low-density ones. As a result, the identical pixel dimensions UI components look larger on low-density screens and smaller on high-density devices. Density-independent pixels, abbreviated as dp (pronounced “dips”), are adaptable units with consistent size on any screen. They offer a versatile approach to fit a design across many platforms. Material UIs employ density-independent pixels to ensure that components appear consistently on displays of varying densities.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :