3

There are so many different units of measurement and when should I use which ones? So far I have found sp, dp, px, in and mm. Are there any more I should know of? Are there any special cases (e.g. Dialogs)?

Other questions don't clearly give examples or explain when or why to use those units.

I know this is off topic, but can some one please answer my other question that is not getting much attention https://stackoverflow.com/questions/25317522/using-a-custom-attribute-for-a-callback-in-a-compound-view-android (I really need an answer)

Community
  • 1
  • 1
user2990508
  • 255
  • 1
  • 4
  • 11
  • Use dp and sp unless you have a really convincing reason to do otherwise. – Steve M Aug 15 '14 at 18:43
  • Check answer and comment. http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android – toidiu Aug 15 '14 at 18:56

1 Answers1

5

Use dp (dip, device independent pixels). These are like pixels, except they treat every device, regardless of its actual pixel density, as if it had a pixel density of, I think 160dpi.

When you are specifying the size of text, use sp. It is to points (the standard way of specifying font size) what dp is to pixels.

Do not ever, ever use px (pixels). Same for pt (points).

If you are writing an application that is a ruler, you have a good reason for using mm and in. Not otherwise.

Better yet, use match_parent and wrap_content!

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40