Search code examples
androidandroid-arrayadapterandroid-image

How to get a list installed app icons


I'm trying to get a list on installed app icons using the following code, however it only shows a list of drawable paths but not the icons (i.e. android.graphics.drawable.BitmapDrawable@417530c8),

Can someone show me what I'm doing wrong.

Thanks

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView appList = (ListView) findViewById(R.id.appList);

    ArrayList<Drawable> cacheArrayList = new ArrayList<Drawable>();

    ArrayAdapter<Drawable> aa = new ArrayAdapter<Drawable>(this,
            android.R.layout.simple_list_item_1, cacheArrayList);

    appList.setAdapter(aa);

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo packageInfo : packages) {
        // cacheArrayList.add((String) packageInfo.loadLabel(pm));
        cacheArrayList.add(packageInfo.loadIcon(pm));

    }

}

}


Solution

  • By default, ArrayAdapter only calls toString on each element of the underlying array. To use an ImageView as well, you need to write a subclass of ArrayAdapter that overrides the getView method. Use a layout that includes an ImageView, then have your overridden getView call setImageDrawable on it, passing the Drawable from the array.