1

If I have any device at hand, how can I know which resources will be used by my apk?
e.g. drawable-hdpi or drawable-xhdpi or drawable-xxhdpi
I am not asking in theory which of these folders is loaded when but specifically if I have a random device at hand how do I know which will be used?

Jim
  • 3,845
  • 3
  • 22
  • 47

3 Answers3

2

You can take a look at some Google results like: generator or explanation if you want to know the 'magic' behind it.

And on the Android development site you can find an overview of the density buckets: Density buckets

If that still doesn't answer your question, you can also take per example a couple of different images per density folder (hdpi, ldpi, mdpi...) and just see which shows on your device or emulator.

Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
  • I though/hoped that it would be as simple as checking something in my device's settings to be honest – Jim Aug 31 '19 at 15:51
2

You have to understand the idea of Density Buckets. Take a look at this image(collected from here) enter image description here

So, Below 160 dpi is called ldpi and found inside folder drawable-ldpi and below 320 and upto 480 dpi is called xhdpi and found inside folder drawable-xhdpi etc.

So, which variation will be used solely depends on your phone's screen density. For better understanding take a look at this article, specially on the Table-1.

To get your phone's density, you can use this code

final float scale = getResources().getDisplayMetrics().density;

So,If your phone falled under xhdpi category, which is between 320 and 480 dpi, drawable-xhdpi variation will be used and so on.

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
  • I though/hoped that to determine the phone's density it would be as simple as checking something in my device's settings to be honest – Jim Aug 31 '19 at 15:52
  • sadly no, and I think its okay because, phone's density is developers need to know knowledge, but not average people's. – touhid udoy Aug 31 '19 at 16:59
  • So if I google for the device's resolution e.g. OnePlus and I see `1080 x 2340 pixels` can I use that information to figure out the resources to be used at runtime reliably? – Jim Aug 31 '19 at 19:37
  • @Jim please ask another question if not directly related to original post. As far as I know, for android PPI and DP are same. so, from screen resolution and device's physical screen size, you can calculate DP or PPI. **PPI = sqrt(height^2 + width^2)/screen_size** – touhid udoy Sep 01 '19 at 03:24
  • So why is this not related to the original post? If I know the PPI can't I also know which of the resources are being used? If no, then sure I can open another question – Jim Sep 01 '19 at 08:57
1

One diagnostic method which is simple is to add a strings.xml(or resource type of your choice) to a qualified res/values- directory with a string value corresponding to the density such as in:

res/values-xxhdpi
    strings.xml (containing)
        <string name="selected_density">xxhdpi</string>

You can do this with other resources types as well such as integer. This lets the system do the work for you. Do this for each density of interest and also supply a default in values/ version.

Then in code simply:

String densityStr =  context.getString(R.string.selected_density);

See Configuration qualifier names table here: https://developer.android.com/guide/topics/resources/providing-resources