Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a shortcut: how can I work with a drawable as Icon?

Below is My code to create a shortcut to a selected application. I have really no problem and the application work quite well.

The problem is that I am able to create a shortcut with a ressource from my application:

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

But I really would like with a custom drawable. ( Drawable myDrawable=.....)

How can I do?

   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's 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();

Thank a lot for any clue

like image 486
Waza_Be Avatar asked Jul 21 '10 11:07

Waza_Be


1 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());
like image 181
Waza_Be Avatar answered Nov 03 '22 00:11

Waza_Be