How can I get the application's icon from the package name?
AndroidAndroid Problem Overview
I have tried various solution from stack overflow with no luck. What I want.
- I know package name of different applications.
- I want to get application Icon from those package name.
- 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?
Android Solutions
Solution 1 - Android
Try this:
try
{
Drawable icon = getContext().getPackageManager().getApplicationIcon("com.example.testnotification");
imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
Solution 2 - Android
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;
}
Solution 3 - Android
This also works:
try
{
Drawable d = getPackageManager().getApplicationIcon(getApplicationInfo());
my_imageView.setBackgroundDrawable(d);
}
catch (PackageManager.NameNotFoundException e)
{
return;
}
Solution 4 - Android
try
{
Drawable drawable = getPackageManager()
.getApplicationIcon("com.whatsapp");
imageView.setImageDrawable(drawable);
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}