Graham Borland
Graham Borland

Reputation: 60701

Android AppWidgetProviderInfo icon id

Android's AppWidgetProviderInfo class contains an integer icon field. I want to get hold of the icon as a Drawable.

I presume the icon id needs to be resolved against some package's Resources. The widget provider is not part of my app, so my own app resources as returned by getResources() are no use. Neither are the system resources given by Resources.getSystem().

How can I convert this integer id into a Drawable?

Upvotes: 2

Views: 833

Answers (2)

Suphi Kaner
Suphi Kaner

Reputation: 59

Like CommonsWare has said you will need to use getResourcesForApplication() here is how you can use it and also since lollipop there is a loadIcon() function that's a lot easier to use

Drawable icon = null;
if (Build.VERSION.SDK_INT >= 21) {
    icon = info.loadIcon(this, density);
} else {
    try {
        String packageName = info.provider.getPackageName();
        Resources resources = context.getPackageManager().getResourcesForApplication(packageName);
        icon = resources.getDrawable(info.icon);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007474

Use PackageManager and getResourcesForApplication().

Upvotes: 1

Related Questions