Thursday, May 24, 2012

Difference of px, dp, dip and sp in android


Can anyone tell what is the difference between px, dip, dp and sp in android?



Source: Tips4all

6 comments:

  1. px is one pixel.
    scale-independent pixels ( sp )
    and density-independent pixels ( dip )
    you want to use sp for font sizes and dip for everything else.
    dip==dp
    from here http://developer.android.com/guide/topics/resources/more-resources.html#Dimension


    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.

    ReplyDelete
  2. Use dp for anything but for fonts. 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

    ReplyDelete
  3. 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

    ReplyDelete
  4. Example at http://blog.edwinevans.me/?p=131

    ReplyDelete
  5. After you demystify the differences. This calculator http://coh.io/adpi/ will make your life easier :)

    ReplyDelete