Issue
Here is how I'm getting the launcher icon for an app (Koltin code but this question is nothing related to Kotlin):
val packageManager = packageManager()
val applicationInfo = packageManager.getApplicationInfo(packageName, 0)
val drawable = applicationInfo.loadIcon(packageManager)
My point now that TaskManager deprecated the use of non-res icons, is to get the value or the DrawableRes of that launcher icon (an identifier that would equals R.drawable.ic_launcher for example).
I couldn't find a way. Is this possible dynamically (or is it impossible due to R.java generation?), without explicitely typing it?
Solution
If you want to retrieve the corresponding resource id of the icon and you know the package name and the item name, you can do it like that:
int resourceId = this.getResources().getIdentifier("nameOfResource", "drawable", this.getPackageName());
"this" is the activity where the code runs on.
"nameOfResource" is the name of the icon itself (without file extension)
"drawable" is the name of the res subfolder, where the icon lies.
You can find the function documentation here
Alternatively you can receive the resource id of an application is this one:
final PackageManager packageManager = getPackageManager();
final ApplicationInfo applicationInfo=packageManager.getApplicationInfo(packageName,PackageManager.GET_META_DATA);
final int appIconResId=applicationInfo.icon;
It is similar to your approach. Your search for all resources of your application and get access the icon id as a member of the information returned.
Answered By - Thomas Richter
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.