I have problems to find out the correct launcher icon size for my shortcut.
On my Nexus 7.2 the value of android.R.dimen.app_icon_size (see code) is 96 pixels. But if I measure the real icon size of other apps on a screenshot of my homescreen, it is 120 pixels. After creating a shortshut, it is smaller (96 px) than all other app icons (120px)
On my Samsung Galaxy SII android.R.dimen.app_icon_size is 72. And this match to my screenshot measure.
Here the result of
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Nexus 7.2:
android.R.dimen.app_icon_size = 96
metrics.dip = 192
metrics.density = 2.0
metrics.densityDpi = 320
metrics.heightPixels = 1824
metrics.scaledDensity = 2.0
metrics.widthPixels = 1200
metrics.xdpi = 320.842
metrics.ydpi = 322.966
Samsung SII:
android.R.dimen.app_icon_size = 72
metrics.dip = 108
metrics.density = 1.5
metrics.densityDpi = 240
metrics.heightPixels = 800
metrics.scaledDensity = 1.5
metrics.widthPixels = 480
metrics.xdpi = 217.71428
metrics.ydpi = 218.49463
Here my code:
// Calculate launcher icon size
int size = (int) getResources().getDimension(android.R.dimen.app_icon_size);
int width = size;
int height = size;
// Create launcher icon bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// Inflate layout to bitmap
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.shortcut, null, false);
// here I edit layout, change ImageView and TextView etc...
layout.setDrawingCacheEnabled(true);
layout.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());
// Create SendFax intent
Intent shortcutIntent = new Intent();
shortcutIntent.setClass(context, SendFax.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create shortcut intent
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, description);
setResult(RESULT_OK, intent);
finish();