4

I am trying to display a list of task in this way -

Icon | ApplicationName | CheckBox

As i found that no listview adapter supports this so i decided to develop a custom adapter but i am unable to fetch the icon of an application. So far i tried this :-

public View getView(int position, View convertView, ViewGroup parent)
{
    View v = convertView;
    View row = inflater.inflate(R.layout.item, parent, false);
    CheckedTextView ctb = (CheckedTextView)row.findViewById(R.id.checkText);
    String pkgName = "com.abc.abc";

    ctb.setText("bla bla");
    ImageView iv = (ImageView)row.findViewById(R.id.image);
    List<PackageInfo> pkgs = activity.getPackageManager().getInstalledPackages(0);
    for(PackageInfo p : pkgs)
    {
       if(p.packageName.equals(pkgName))
       {
          Drawable d =  p.applicationInfo.loadIcon(pm);
           iv.setImageDrawable(d);
       }
    }

     return row;
}
}

And i am sure i am trying something stupid in this code. Please try to catch whatever i am doing wrong.

Thanks.

Varundroid
  • 9,135
  • 14
  • 63
  • 93

2 Answers2

9

You can get an app's (package's) icon with:

String pkg = "com.app.my";
Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
bigstones
  • 15,087
  • 7
  • 65
  • 82
  • lol. cheers mate. and please help me little more as i am stuck again :P my custom adapter has checkbox too and i just defined it in my custom adapter's XML and did noting with it in my custom adapter class. but the problem is how to know that checkbox is checked or not checked in the program where i am actually using this custom adapter. Do i need to bind it with some code? i used simple_list_item_multiple_choice before and i want the same working. is it possible? please help if u know how to do it otherwise i really appreciate your efforts. Thanks :) – Varundroid Apr 10 '11 at 15:13
  • @Varundroid: I have no experience with that actually, but I had to put a button in my custom list item. I ended up creating a custom view, inflating from xml in its constructor and adding convenience methods to access the button (like a `MyView.getButton()` method). – bigstones Apr 10 '11 at 15:26
  • no worries still thanks for telling me this button stuff may be in future i may need to use it somewhere. :) – Varundroid Apr 10 '11 at 15:36
3

Please check out my solution for getting the list of icon of application

In this getting the list of ResolveInfo of all application.

resolveInfo class contain Information that is returned from resolving an intent against an IntentFilter. This partially corresponds to information collected from the AndroidManifest.xml's tags

This is the way to get the list of icon of application in android, hope this example will be help for you

    import java.util.List;


import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class IconListActivity extends ListActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadApps();  

    }



    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> mApps = getPackageManager().queryIntentActivities(mainIntent, 0);

        ListView listView = getListView();
        listView.setAdapter(new AppsAdapter(this,mApps));
     }

    public class AppsAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<ResolveInfo> mApps;

        public AppsAdapter(Context context, List<ResolveInfo> mApps) {
            this.inflater = LayoutInflater.from(context);
            this.mApps = mApps;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHendler hendler;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.row_view, null);
                hendler = new ViewHendler();
                hendler.textLable = (TextView)convertView.findViewById(R.id.textViewLable);
                hendler.iconImage = (ImageView)convertView.findViewById(R.id.imageViewIcon);
                convertView.setTag(hendler);
            } else {
                hendler = (ViewHendler)convertView.getTag();
            }
            ResolveInfo info = this.mApps.get(position);
            hendler.iconImage.setImageDrawable(info.loadIcon(getPackageManager()));
            hendler.textLable.setText(info.loadLabel(getPackageManager()));

            return convertView;

        }
        class ViewHendler{
            TextView textLable;
            ImageView iconImage;
        }


        public final int getCount() {
            return mApps.size();
        }

        public final Object getItem(int position) {
            return mApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }

}
Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78