1

I want to print sizes of all drawables at run-time. So if I am on hdpi device then I can print the size of hdpi drawables but how to get access to, lets say mdpi and xhdpi as well? I can get access to all drawables resource ids with following code:

final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();

for (int i = 0, max = fields.length; i < max; i++) {
    final int resourceId;
    try {
        resourceId = fields[i].getInt(drawableResources);
    } catch (Exception e) {
        continue;
    }
    /* make use of resourceId for accessing Drawables here */
}
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • You can **calculate** them. – Phantômaxx May 15 '15 at 07:46
  • 2
    Ok consider a scenario where I have a huge app and I realized that developers messed up by placing wrong sizes in different folders. So now I want to print actual sizes for drawables embed in my app for different screen sizes. Anyway I just found that I can do `context.getResources().getDrawableForDensity(resourceId, DisplayMetrics.DENSITY_MEDIUM)` or invoke same with `DisplayMetrics.DENSITY_XXHIGH` etc – M-Wajeeh May 15 '15 at 07:52
  • Gonna post it as answer after testing it. – M-Wajeeh May 15 '15 at 07:53

1 Answers1

3

Ok I found it, Basically you explicitly ask for a particular density drawable like this:

Drawable drawable = resources.getDrawableForDensity(id, DisplayMetrics.DENSITY_XHIGH);

or preferably this version

Drawable drawable = resources.getDrawableForDensity(id, DisplayMetrics.DENSITY_XHIGH, theme);
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103