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;
}