Difference between px, dp, dip and sp in Android?
3
Can anyone explain the differences between the px, dip, dp and sp units in Android?
improve this question | comment
Krystina Shields Created at: 2013-11-13 17:07:27 UTC By Krystina Shields
After you demystify the differences. This calculator coh.io/adpi will make your life easier :) - Alexzander Kunze
this is use full difference between px, dip, dp and sp in android [ developer.android.com/guide/topics/resources/… - Michael Monahan
There's no "dip". I guess anyone who says "dip" means "dp" and doesn't know better? - Ms. Crystel Olson
"dp" was actually called "dip" in early versions of the SDK. They changed it to "dp" shortly after. - Rowena Zboncak
@Jonny: "I guess anyone who says "dip" means "dp" and doesn't know better?" I'm reading the book "The Busy Coder's Guide to Android" by Mark Murphy (@CommonsWare here at SO, with 300,000 reputation), and he uses dip. - Joany Price
12 Answers
0
I have calculate below formula to make convertions  dpi to dp & sp

0
Basically the only time where px applies is 1px, and that's if you want exactly 1 pixel on the screen like in the case of a divider. On >160, you may get 2-3 pixels, and on 120dpi, it rounds to 0. 
0
Use dp for anything but for fonts use sp.

Pretty much everything about this and how to achieve the best support for multiple screens with different sizes and density is very well documented here:

Supporting Multiple Screens
If you are any serious about developing an android app for more than one type of device, you should have read the above at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.

Screen Sizes and Densities
0
For some screenshots on different devices showing the differences between each unit (as well as some curious discrepancies against the documentation), see this related question:

Difference between android dimension: pt and dp
0
px
Pixels - point per scale  corresponds to actual pixels on the screen.

in
Inches - based on the physical size of the screen.

mm
Millimeters - based on the physical size of the screen.

pt
Points - 1/72 of an inch based on the physical size of the screen.

dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

Take the example of two screens that are the same size but one has a resolution of 160 dpi (dots per inch, i.e. pixels per inch) and the other is 240 dpi.

                          Lower resolution   screen          Higher resolution, same size
Physical Width                      1.5 inches                        1.5 inches
Dots Per Inch (“dpi”)               160                               240
Pixels (=width*dpi)                 240                               360
Density (factor of baseline 160)    1.0                               1.5
Density-independent Pixels          240                               240
(“dip” or “dp” or “dps”)
Scale-independent pixels (“sip” or “sp”)    Depends on user font size settings  same

0
Moreover you should have clear understanding about the following concepts:


  Screen size: Actual physical size, measured as the screen's diagonal. For simplicity, Android groups all actual screen sizes into
  four generalized sizes: small, normal, large, and extra large.
  
  Screen density: The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a
  "low" density screen has fewer pixels within a given physical area,
  compared to a "normal" or "high" density screen. For simplicity,
  Android groups all actual screen densities into four generalized
  densities: low, medium, high, and extra high.
  
  Orientation: The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the
  screen's aspect ratio is either wide or tall, respectively. Be aware
  that not only do different devices operate in different orientations
  by default, but the orientation can change at runtime when the user
  rotates the device.
  
  Resolution: The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly
  with resolution; applications should be concerned only with screen
  size and density, as specified by the generalized size and density
  groups.
  
  Density-independent pixel (dp): A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or
  position in a density-independent way. The density-independent pixel
  is equivalent to one physical pixel on a 160 dpi screen, which is the
  baseline density assumed by the system for a "medium" density screen.
  At runtime, the system transparently handles any scaling of the dp
  units, as necessary, based on the actual density of the screen in use.
  The conversion of dp units to screen pixels is simple: px = dp * (dpi
  / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical
  pixels. You should always use dp units when defining your
  application's UI, to ensure proper display of your UI on screens with
  different densities.


Reference: Android developers site
0
px is one pixel.
 scale-independent pixels ( sp )
 and density-independent pixels ( dip ).

You would use sp for font sizes and dip for everything else.

dip==dp


from here 


  px
  Pixels - corresponds to actual pixels on the screen.  
  
  in
  Inches - based on the physical size of the screen.  
  
  mm
  Millimeters - based on the physical size of the screen.  
  
  pt
  Points - 1/72 of an inch based on the physical size of the screen.  
  
  dp
  Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".  
  
  sp
  Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

0
px

Pixels - corresponds to actual pixels on the screen.

dp or dip

Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen.

Use of dp:

Density independence -
Your application achieves “density independence” when it preserves the physical size (from the user’s point of view) of user interface elements when displayed on screens with different densities. (ie) The image should look the same size (not enlarged or shrinked) in different types of screens.

sp

Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference.

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
0
Anything related with the size of text and appearance must use sp

whereas,  anything related to the size of the controls or the layouts etc. must be used with dp.

you can use both dp and dip at its places.
0
For completeness: Roman Nurik explains DP in one of his Design.Devbytes series.

http://www.youtube.com/watch?v=zhszwkcay2A
0
Difference between dp and sp units mentioned as "user's font size preference" by the answers copied from official documentation can be seen at run time by changing Settings->Accessibility->Large Text option.

Large Text option forces text to become 1.3 times bigger.

private static final float LARGE_FONT_SCALE = 1.3f;


This might be well of course vendor dependent since it lies in packages/apps/Settings.
0
I will elaborate more on how exactly does dp convert to px:

If running on hdpi device 150x150 px image will take up 100*100 dp of screen space.
If running on mdpi device 150x150 px image will take up 150*150 dp of screen space.
If running on xhdpi device 150x150 px image will take up 75*75 dp of screen space.
The other way around: say, you want to add an image to your application and you need it to fill 100*100 dp control, you'll need to create different size images for supported screen sizes:

100*100 px image for mdpi
150*150 px image for hdpi
200*200 px image for xhdpi
Your Answer