0

i have an app that creates shortcuts. it generates the shortcut icon dynamically, so i need to know the correct launcher icon size.

to handle this, i created dimens.xml in values-ldpi/mdpi/hdpi/xhdpi/xxhdpi and defined my icon size to be 36/48/72/96/144px respectively.

this scheme works, except on 10", xhdpi tablets (like the nexus 10). it appears these tablets use a launcher icon size of 144px (xxhdpi) despite have an xhdpi screen.

is there a way to correctly detect the launcher icon size that takes into account 10" xhdpi tablets? or is there a better scheme for getting my icons sized correctly? or perhaps is there a way to differentiate this case from the simple xhdpi case?

thanks.

Jeffrey Blattman
  • 21,054
  • 8
  • 74
  • 127
  • "it appears these tablets use a launcher icon size of 144px (xxhdpi) despite have an xhdpi screen" -- yes, Google mentioned this, albeit through non-official channels: https://plus.google.com/118292708268361843293/posts/ePQya3KsTjW Perhaps `getLauncherLargeIconDensity()` on `ActivityManager` will help you, as Nick mentioned in his G+ post. – CommonsWare Jan 25 '13 at 00:02
  • If you're interested, have a look at http://stackoverflow.com/questions/13215587/android-xxhdpi-resources for a run-down of why the Nexus 10 uses XXHDPI resources. – Eric Jan 25 '13 at 00:06
  • @CommonsWare please post this as an answer (getLauncherLargeIconDensity()), that worked. – Jeffrey Blattman Jan 25 '13 at 00:30
  • Actually, since I didn't fully understand your problem, let alone precisely if and how `getLauncherLargeIconDensity()` would help, I'd recommend that you answer your own question in this case. It'll be more useful to others who run across this question. – CommonsWare Jan 25 '13 at 00:35

1 Answers1

1

Answering my own questions.

To get the launcher icon size, simply call ActivityManager.getLauncherLargeIconSize() as suggested by CommonsWare above. One slight hiccup is that this is only available on API 11+. In that case, fall back to using DisplayMetrics. This will of course fail if there was a 10" XHDPI device that ran android 2, which is extremely unlikely (since X*HDPI didn't exist at the time of Android 2). Here's the utility method i wrote,

@SuppressLint("NewApi")
private int getLauncherIconSize() {
    int size = 48;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActivityManager mgr = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        size = mgr.getLauncherLargeIconSize();
    } else {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            size = 36;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            size = 48;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            size = 72;
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            size = 96;
            break;
        case DisplayMetrics.DENSITY_XXHIGH:
            size = 144;
            break;
        }
    }

    return size;
}
Jeffrey Blattman
  • 21,054
  • 8
  • 74
  • 127