Wednesday, April 4, 2012

Android: screen densities and sizes. How to calculate them

For me it has always be complex to find out, for each android device, what is its density and its screen size. I'll try to explain in this article what i've discovered so far.
It is really very important to find out these 2 parameters, since Android has a way to distinguish which resources to use (both xml layout files and drawables) based on such parameters.

In detail, Android allows you to specify your drawable resources based on the screen density of your phone. The 4 screen densities available are:


  • ldpi - 120 dpi
  • mdpi - 160 dpi
  • hdpi - 240dpi
  • xhdpi - 320 dpi
Each specifies a different screen density, i.e. a different amount of pixels for square inch of screen. These are called dpi or dots per inch.

For example, a HTC Desire has a density of 240dpi, i.e there are 240 pixels for each square inch of screen.
A LG Optimus One has a density of 160dpi, i.e. there are 160 pixels for each square inch of screen. Obviously, if we provide the same icon on these devices, it will show itself larger or the LG than on the HTC phone. A way to cope with that is to specify a layout that uses density independent pixels. But that's another story.

Let's focus on the dpi. How can we calculate this value? Let's take the two parameters that we always know of a device: diagonal in inches and screen size in pixels.
The HTC Desire has: 800 x 480 and 3.7''.
dpi = sqrt(w^2 + h^2)/d = sqrt(800^2 + 480^2)/3.7 = 252,1

So the screen density of HTC Desire is HDPI.

the display size is important because usually we associate different layouts to different screen sizes. A device can have 4 screen sizes:

  • small-screen
  • normal-screen
  • large-screen
  • xlarge-screen
On the Android dev website, they tell us that a medium screen is more or less between 3 and 5''. But is there a scientific way to calculate it?
They also tell us that:


  • small-screens must have a screen size of 426 x 320 dp = 136320 dp
  • normal-screens must have a screen size of 470 x 320 dp = 150400 dp
  • large-screens must have a screen size of 640 x 480 dp = 326400 dp
  • xlarge-screens must have a screen size of 960 x 720 dp = 691200 dp
Usually phones are in the small and normal category, while tablets are in the large and xlarge category, although there are some important exceptions to this rule.



How to calculate the display size for a device? Let's take the HTC Desire as example.
The HTC Desire has: 800 x 480 and 3.7'' and is a HDPI device with a scale of 1,57, so each virtual pixel counts as 1.57 physical pixels. This is known as scale and can be determined as: 252/160. A device with 160 dpi has a scale of 1 (1 physical pixel counts a s1 virtual pixel). This was the density of the first Android phone.

So: 800/1,57 = 510 dp; 480/1,57 = 305 dp
520 * 305 = 155550 dp which is higher than 150400 but lower than 326400. So the HTC Desire has  a normal screen.

A particular case is give by the recent Galaxy Note. This device has: 1280x800 5.3'' display.
So the DPI is 284,8. Its scale ratio is 1.77. It is between 1.5 and 2. Aaagh.. So in what screen density folder should we put the drawables? A search on stackoverflow suggest that the Note si considered as XHDPI screen.
But here's the problem. We said that phones have a small or medium size screen. This is WRONG for the Galaxy Note.
Infact: 1280/1,77 = 724 dp;  800/1,77 = 451
724 * 451 = 326524 which is larger than 326400

So if you want to filter your app based on the screen size, be careful. Some phones, like the Galaxy Note, belongs to the Large screens device family!



7 comments:

  1. hi,

    thank you for that great article,
    There is a small suggestion to avoid some confusion instead of dp use dpi to denote dots per inches, as you could easily confuse dp with decimal independent pixels.
    Once again thank you,

    Kesari

    ReplyDelete
    Replies
    1. hi kesari, thanks for your comment, I will check :)

      Delete
    2. Hello kesari.
      Actually what he wrote was correct it is dp(density independent pixel) and not dpi.
      Let me clarify this.
      Basically pixels = dp * density ratio.Here density ratio is called as scale.
      So dp decides the screen size of the device according to the given values.
      so dp = pixels/density ratio.





      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Really impressive & helpful blog for me, I was searching this kind of information from a long time. Thanks for help me.
    http://www.appdevelopmentcompany.com.au

    ReplyDelete
  4. Really very helpful blog for me,
    Thanks for help me.

    ReplyDelete
  5. very interesting post.this is my first time visit here.i found so many interesting stuff in your blog especially its discussion..thanks for the post! Authorized Dealer for Develop & Espon

    ReplyDelete