How to find what density bucket the phone is in?
Asked Answered
R

3

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.

Raulrausch answered 30/1, 2015 at 16:0 Comment(0)
S
-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.

Susurrus answered 30/1, 2015 at 16:16 Comment(5)
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.Aerometry
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.Susurrus
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.Aerometry
Aah. I see what you mean now. You are right. This shouldn't be the accepted answer for this particular question then.Susurrus
Does not really answer the question, also the snippet is not related to question at all.Foundling
C
13

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 );
Callison answered 30/1, 2015 at 16:12 Comment(4)
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 :)Raulrausch
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.Callison
I am sorry if I was a little unclear. I updated my question. +1 because I caused you some inconvenience.Raulrausch
a very creative solution (to add the resources under different values)Rodgerrodgers
A
11

Hey I recognize that chart! Mostly because I created it [archive link] :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 [archive link], 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.

Aerometry answered 5/2, 2016 at 20:25 Comment(2)
manufacturers choose density buckets? I always thought it was the Android runtime that chose the bucketRaulrausch
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).Aerometry
S
-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.

Susurrus answered 30/1, 2015 at 16:16 Comment(5)
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.Aerometry
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.Susurrus
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.Aerometry
Aah. I see what you mean now. You are right. This shouldn't be the accepted answer for this particular question then.Susurrus
Does not really answer the question, also the snippet is not related to question at all.Foundling

© 2022 - 2024 — McMap. All rights reserved.