5

Slightly noob-ish question but I get confused when it comes to calculating what density bucket the phone falls in. I'll take my Galaxy S3 as an example.

It has a resolution of 1280 x 720 which means it has 306 dpi. Now, referring to the chart below, my phone falls in the hdpi category because it has more than 240 dpi but less than the 320 dpi needed to be an xhdpi screen. So, the phone is 853 x 480 DP (dividing by 1.5)

However, a screen information app on my phone tells me that it is an xhdpi screen. So, the phone is 640 x 360 DP (dividing by 2).

How do I know what correct density bucket my phone falls in?

enter image description here

Update:
I am trying to design my app for the top 10 Android phones in my country. So I am calculating their sizes in DPs to design UIs based on their "smallest width DPs". This isn't a one-off size calculation.

An SO User
  • 24,612
  • 35
  • 133
  • 221

3 Answers3

12

If you want to know it once, you can get an app like ScreenInfo and run it. If you want to know programmatically, you can add density folders, such as values-mdpi and values-xhdpi, place an xml file in it, say whoami.xml with the following content:

<resources>
    <string name="density_bucket">mdpi</string>
</resources>

And, respectively:

<resources>
    <string name="density_bucket">xhdpi</string>
</resources>

and then inquire from the app:

String densityBucket = getResources().getString( R.string.density_bucket );
323go
  • 14,143
  • 6
  • 33
  • 41
  • I am trying to design for the top 10 Android phones in my country. I need to know *prior* what their size in DP is so I can design appropriate layouts. Your answer does not help :) – An SO User Jan 30 '15 at 16:14
  • 1
    I answered the question you asked. Did you ask the wrong question? Nowhere in your question does it say you are requesting a recommendation for an off-site resource, which would have been off-topic. – 323go Jan 30 '15 at 16:16
  • I am sorry if I was a little unclear. I updated my question. +1 because I caused you some inconvenience. – An SO User Jan 30 '15 at 16:17
11

Hey I recognize that chart! Mostly because I created it :D. To clarify, the chart shows what dpi each density bucket is baselined to, but cannot be used to determine which density bucket a device should be classified as. That said, this illustration below shows how devices are generalized into density buckets, and you can see they may have a higher or lower dpi than the bucket they fall in.

Density bucket generalization

Now to answer your question, unfortunately, there is no guaranteed way to know what density bucket a device will fall into purely based off of its specs. This is because the device manufacturer is able to choose the density bucket. Most of the time, they will choose the bucket closest to the actual dpi of the device**. Luckily, Google has gathered a list of common devices, along with their screen dimensions in dp, and density buckets. This should give you an idea of how to properly support the most prevalent screen size that are out in the wild.

**Note: Some devices have their own special density buckets. Most notably, the Nexus 7 (2012) is 213dpi (tvdpi), the Nexus 5X is 420dpi, and the Nexus 6 and 6P are 560dpi. These devices grab assets from the other density versions and scale them.

Steven Byle
  • 13,149
  • 4
  • 45
  • 57
  • manufacturers choose density buckets? I always thought it was the Android runtime that chose the bucket – An SO User Feb 06 '16 at 20:35
  • Manufacturers choose which device bucket a device falls into, and the Android runtime decides how to get the proper asset (if it doesn't exist for that density, it creates it). – Steven Byle Feb 07 '16 at 05:12
-1

I think Android tries to find the "best match". So if 306 is in between 240 and 320, its much more closer to 320. So it will use xhdpi. You can read more here. Quoting from the official link to docs:

Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with the hdpi qualifier (such as drawable-hdpi/) might be the best match, so the system uses the drawable resource from this directory.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • The question is asking how to determine which density bucket a device falls in. This snippet is explaining how the Android OS decides which asset to get based on asset and device screen density. Also, the device is classified by the manufacturer, there is "no best" match going on, the device's density bucket is hardcoded. – Steven Byle Feb 05 '16 at 20:29
  • Thanks for your comment Steven. It makes sense. However lets say the `device's density bucket is hardcoded` to be `hdpi`. What happens in case when one doesn't include an `hdpi` image/drawable but the images are included in other dpi folders? Its not like the app will not show the image/drawable. Is it? In this case it will use the "best matching" drawable from one of the other folders and display it. – Shobhit Puri Feb 05 '16 at 21:03
  • Correct, Android would use the `hdpi` asset, or if it didn't exist, it would create it by scaling another version. However, the original question was not about how to determine which density version of assets to used. It was about how to determine what density bucket a device falls into. – Steven Byle Feb 07 '16 at 04:58
  • Aah. I see what you mean now. You are right. This shouldn't be the accepted answer for this particular question then. – Shobhit Puri Feb 07 '16 at 05:39
  • Does not really answer the question, also the snippet is not related to question at all. – Behzad Bahmanyar Jun 02 '16 at 05:43