Creating a shortcut: how can I work with a shortcut as an icon?

Below my code creates a shortcut for the selected application. I really have no problem and the application works quite well.

The problem is that I can create a shortcut with ressource from my application:

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 

But I really would like with the help of a user feature. (Drawable myDrawable = .....)

How can i do this?

  ResolveInfo launchable=adapter.getItem(position); final Intent shortcutIntent = new Intent(); ActivityInfo activity=launchable.activityInfo; ComponentName name=new ComponentName(activity.applicationInfo.packageName,activity.name); shortcutIntent.setComponent(name); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); final Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // Sets the custom shortcut title intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, launchable.loadLabel(pm)); // Set the custom shortcut icon intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); // add the shortcut intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); sendBroadcast(intent); finish(); 

Thanks so much for any clue

+6
android android-intent drawable icons shortcut
source share
2 answers

Finally found a solution; I was stupid to use Intent.EXTRA_SHORTCUT_ICON_RESOURCE:

Here is the correct code:

 Drawable iconDrawable = (....); BitmapDrawable bd = (BitmapDrawable) iconDrawable; intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap()); 
+26
source share

I can not comment, since I have only 23 reputations, but I used your code and it was useful. But my image did not scale correctly on the shortcut, so I found two interesting topics that could complete the solution:

To get the correct size for the shortcut icon: fooobar.com/questions/870412 / ...

To scale the popped (using the size calculated with the previous function) fooobar.com/questions/28860 / ...

With all this I got a shortcut with a properly scaled icon, hope this helps

+3
source share

All Articles