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!