72

I have tried various solution from stack overflow with no luck. What I want.

  1. I know package name of different applications.
  2. I want to get application Icon from those package name.
  3. Show those icons in Image View.

For example, I have a package name com.example.testnotification. How to get this apps icon and show it in an ImageView?

peterh
  • 11,875
  • 18
  • 85
  • 108

5 Answers5

158

Try this:

try
{
    Drawable icon = getContext().getPackageManager().getApplicationIcon("com.example.testnotification");
    imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException e)
{
    e.printStackTrace();
}
user7418129
  • 1,074
  • 14
  • 18
T_V
  • 17,440
  • 6
  • 36
  • 48
13

If you want to get the icon (picture) of any installed application from its package name, then just copy and paste this code. It will work:

try
{
    Drawable d = getPackageManager().getApplicationIcon("com.AdhamiPiranJhandukhel.com");
    my_imageView.setBackgroundDrawable(d);
}
catch (PackageManager.NameNotFoundException e)
{
    return;
}
Bondolin
  • 2,793
  • 7
  • 34
  • 62
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
9

This also works:

try
{
    Drawable d = getPackageManager().getApplicationIcon(getApplicationInfo());
    my_imageView.setBackgroundDrawable(d);
}
catch (PackageManager.NameNotFoundException e)
{
    return;
}
thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
2

I tried all of the above answers, but was only getting icons from system apps, issue faced in Android 11 (API level 30) or higher, a little digging led me to this docs page which said that you need to add the following permission in your AndroidManifest file:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
    tools:ignore="QueryAllPackagesPermission" />

And now all icons are visible.

I used the following piece of code to implement the said functionality using DataBinding:

@BindingAdapter("getImageFromPackageName")
fun ImageView.getImageFromPackageName(packageName: String) {
    try {
        val icon = context.packageManager.getApplicationIcon(packageName)
        this.setImageDrawable(icon)
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
}

Hope that helps, Thanks!

Mihir Shah
  • 51
  • 3
1
try
{
    Drawable drawable = getPackageManager()
            .getApplicationIcon("com.whatsapp");
    imageView.setImageDrawable(drawable);
}
catch (PackageManager.NameNotFoundException e)
{
    e.printStackTrace();
}
pirho
  • 11,565
  • 12
  • 43
  • 70
saigopi.me
  • 14,011
  • 2
  • 83
  • 54