Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting App Icon in Android

Tags:

android

I'm having some trouble retrieving icons for installed Android App. The following code returns icons for all apps, however, some apps are returned with small icons (perhaps from drawable - ldpi folder) while some with large icons.

List<PackageInfo> package = getPackageManager().getInstalledPackages(0);

for(PackageInfo p : package){
    iApp.icon = p.applicationInfo.loadIcon(getPackageManager()); 
}

How Can I get icons which are all of the same size, just like in Manage Application section of Android?

like image 873
Raunak Avatar asked Jan 05 '11 04:01

Raunak


People also ask

Why has my app icon disappeared?

If the missing app is not showing in your App Library, which means it's no longer on your device. Perhaps, you have uninstalled it unconsciously. In that case, all you need is to search for the app on the App Store and reinstall it. Doing that will bring back the app icon to your home screen and the App Library.

How do I put an app icon?

(Android studio)Go to menu File* → New → Image Assets → select launcher icon → choose image file. It will automatically re-size. Done!

How do I find missing apps icon on Android?

Check Your App Drawer The “App drawer” is usually the best place to start if you're missing essential icons on your device. If you're not sure, the app drawer is the icon in the middle of your bottom dock, that opens a menu containing most of the apps found on your device.


2 Answers

I want to add something to previous answer what applies to Android 3.0 and greater.

You can notice that icons retrieved in this standard way (i mean applicationInfo.loadIcon and other typical methods) are smaller than icons you see in launcher app. Scale up just makes icons blurrier. If you want big icons you can use next code (i took it from launcher source code you can find here and changed a bit). Pay your attention to activityManager.getLauncherLargeIconDensity method.

public Drawable getFullResDefaultActivityIcon() {
    return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
}

public Drawable getFullResIcon(Resources resources, int iconId) {
    Drawable d;
    try {
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        int iconDpi = activityManager.getLauncherLargeIconDensity();
        d = resources.getDrawableForDensity(iconId, iconDpi);
    } catch (Resources.NotFoundException e) {
        d = null;
    }

    return (d != null) ? d : getFullResDefaultActivityIcon();
}

public Drawable getFullResIcon(String packageName, int iconId) {
    Resources resources;
    try {
        resources = mContext.getPackageManager().getResourcesForApplication(packageName);
    } catch (PackageManager.NameNotFoundException e) {
        resources = null;
    }
    if (resources != null) {
        if (iconId != 0) {
            return getFullResIcon(resources, iconId);
        }
    }
    return getFullResDefaultActivityIcon();
}

public Drawable getFullResIcon(ResolveInfo info) {
    return getFullResIcon(info.activityInfo);
}

public Drawable getFullResIcon(ActivityInfo info) {
    Resources resources;
    try {
        resources = mContext.getPackageManager().getResourcesForApplication(info.applicationInfo);
    } catch (PackageManager.NameNotFoundException e) {
        resources = null;
    }
    if (resources != null) {
        int iconId = info.getIconResource();
        if (iconId != 0) {
            return getFullResIcon(resources, iconId);
        }
    }
    return getFullResDefaultActivityIcon();
}

private Drawable getAppIcon(ResolveInfo info) {
    return getFullResIcon(info.activityInfo);
}

Hope it helps someone.

like image 52
Andrei Buneyeu Avatar answered Sep 19 '22 15:09

Andrei Buneyeu


Apps might just have different sized icons. The Android Settings app loads icons just like you. It scales the icons when displaying them.

Here's a snippet from packages/apps/Settings/res/layout/manage_applications_item.xml:

<ImageView android:id="@+id/app_icon"
    android:layout_width="@android:dimen/app_icon_size"
    android:layout_height="@android:dimen/app_icon_size"
    android:layout_marginRight="11dip"
    android:layout_gravity="center_vertical"
    android:scaleType="fitCenter"/>
like image 33
rascal2210 Avatar answered Sep 19 '22 15:09

rascal2210