2

I am trying to get the installed application icon in my list of the users installed applications for a checkbox as seen here:

        addCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               if (addCheckbox.isChecked()){
                   System.out.println("Checked");
// GET ITEM ICON HERE
               }else{
                   System.out.println("Un-Checked");
               }

           }});

In my list, I have this set up for each listView item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip" 
 >

<ImageView
  android:id="@+id/ivIcon"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_marginRight="5dip"
  android:scaleType="center"
  android:contentDescription="@string/desc"
/>

<LinearLayout
  android:orientation="vertical"
  android:layout_width="0dip"
  android:layout_height="fill_parent"
  android:layout_weight="1"    
>

  <TextView
      android:id="@+id/tvName"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:gravity="center_vertical"         
  />

  <TextView
      android:id="@+id/tvPack"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:singleLine="true"
      android:ellipsize="marquee"         
  />

</LinearLayout>

<CheckBox
  android:id="@+id/addCheckbox"
  android:layout_width="0dp"
  android:layout_height="fill_parent"
  android:layout_weight="0.3"
  android:gravity="center_vertical" 
/>

</LinearLayout>

So I tried getting the listView item and then the icon by doing this (like I do in my other class):

        // implement event when an item on list view is selected
    mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        }

    });

But I didn't know how to implement it so that I could get the selected item from the list so that I could get the icon (if I even need to do that or if I can just use the icon from my set up of the listView items).

I am trying to get the icon so that I can use it in a gridView.

Am I going about this the right way? Or else how can I do this? (If you need to see any more code let me know!)

FURTHER EXPLANATION:

So to get the list of installed applications, I have a class that gets the information for the list and then a custom adapter. Here is the Utlities.java (part where I get the information):

public class Utilities {

/*
 * Get all installed application on mobile and return a list
 * @param   c   Context of application
 * @return  list of installed applications
 */
public static List<?> getInstalledApplication(Context c) {
    return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}

Then I have a custom adapter here that sorts the information based on a layout: AppInfoAdapter.java:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class AppInfoAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ApplicationInfo> originalListAppInfo;
private Filter filter; 

public AppInfoAdapter(Context c, List<ApplicationInfo> listApp, PackageManager pm) {
    mContext = c;
    this.originalListAppInfo = this.mListAppInfo = listApp;
    mPackManager = pm;
    }

@Override
public int getCount() {
    return mListAppInfo.size();
}

@Override
public Object getItem(int position) {
    return mListAppInfo.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // get the selected entry
    ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
    }

    // load controls from layout resources
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
    final CheckBox addCheckbox = (CheckBox)v.findViewById(R.id.addCheckbox);

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.loadLabel(mPackManager));
    tvPkgName.setText(entry.packageName);
    addCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               if (addCheckbox.isChecked()){
                                       System.out.println("Checked");
                   PackageManager pm = mContext.getPackageManager();
                   Drawable icon = pm.getApplicationIcon(apk.package_name);
                   Drawable default_icon = pm.getDefaultActivityIcon();
                   if (icon instanceof BitmapDrawable && default_icon instanceof BitmapDrawable) {
                       BitmapDrawable icon_bd = (BitmapDrawable)icon;
                       Bitmap icon_b = icon_bd.getBitmap();
                       BitmapDrawable default_bd = (BitmapDrawable)pm.getDefaultActivityIcon();
                       Bitmap default_b = default_bd.getBitmap();
                       if (icon_b == default_b) {
                           // It's the default icon
                       }
               }else{
                   System.out.println("Un-Checked");
               }

           }});


    // return view
    return v;
}

So in here, I have to fix the lines:

Drawable icon = pm.getApplicationIcon(apk.package_name);

so that I can get the icon via the package name. That's why I am trying to import and use in my "like I do in my other classes(above)" part so that I can get the position of the listView item and then get the icon.

Note: I've tried something like this:

Drawable icon = pm.getApplicationIcon(AppInfoAdapter.getItem(position).packageName);

but it doesn't work and just causes a mess of errors.

New error:

When I changed these lines:

        ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.loadLabel(mPackManager));

to this:

        ivAppIcon.setImageDrawable(entry.applicationInfo.loadIcon(mPackManager));
    tvAppName.setText(entry.applicationInfo.loadLabel(mPackManager));

I get an error here:

                            Drawable icon = pm
                                .getApplicationIcon(entry.packageName);

saying to surround it with a try and catch statement so I do this:

Drawable icon = null;
                        try {
                            icon = pm
                                    .getApplicationIcon(entry.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
user2909006
  • 253
  • 6
  • 22

1 Answers1

3

I'm not entirely sure what you're trying to do, but I get the idea that you don't know how to get another application's icon. A quick bit of Googling got me to this code:

PackageManager manager = mContext.getPackageManager();
Drawable appIcon = manager.getApplicationIcon("com.google.maps");

Which I think will do what you want - you then use the drawable in your gridview(that's the part of your question I'm unclear about)

edit

ok, I've had a look at what you're doing more closely now. You're getting the list of applications using

getInstalledApplications(PackageManager.GET_META_DATA)

according to this, that will return a List<ApplicationInfo> which is not necessarily what you want in this situation. This question asks what the difference between ApplicationInfo and PackageInfo is, and the very first answer tells me that "ApplicationInfo is actually a field/attribute of PackageInfo". So now I know that you're immediately making things harder for yourself by limiting the information you have to play with. Instead I would call getInstalledPackages which will return a List<PackageInfo. From this, if you wanted later on, you could get the same information you currently have by getting the applicationInfo field. I'd change the code in the following way:

/*
 * Get all installed application on mobile and return a list
 * @param   c   Context of application
 * @return  list of installed applications
 */
public static List<PackageInfo> getInstalledApplications(Context c) {
return c.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

}

and change all instances of ApplicationInfo to PackageInfo in AppInfoAdapter. For example...

// get the selected entry
PackageInfo entry = (PackageInfo) mListAppInfo.get(position);

Then you can replace

Drawable icon = pm.getApplicationIcon(apk.package_name);

with

Drawable icon = pm.getApplicationIcon(entry.packageName);

See how that goes...

Community
  • 1
  • 1
roarster
  • 4,058
  • 2
  • 25
  • 38
  • So just to clarify, I have a list of all the users installed applications and in each listView item there is a checkbox, that when checked would get the listView's application icon and then turn it into a drawable like you said. But instead of using "com.google.maps" for example, since I can't be sure of what applications the user would have installed, what else could I try? And to help with the gridView part, so after I collect the icon and turn it into a bitmap, I want to send it into a gridView nearby where the user can look at what icons they have "selected" from my list of installed apps. – user2909006 Oct 26 '13 at 01:08
  • How are you getting hold of the user's installed applications? Surely in order to do so you have to know the package name(e.g com.google.maps) of each app. And for the gridview part, it doesn't sound very unusual. Here's a tutorial on displaying images in a gridview: http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/ I would give you code, but all I would be doing is copying what's on that page – roarster Oct 26 '13 at 09:57
  • Ok, so I was able to get the gridView set up with the link you gave me (thanks by the way!), so now I guess the question is how can I take the icon and make it usable in the gridView. So then to answer your question, please check the updated question under "FURTHER EXPLANATION" Hopefully that helps. – user2909006 Oct 27 '13 at 16:45
  • I also updated some coding so please also look at that in my onCheckedChanged() method. – user2909006 Oct 27 '13 at 17:51
  • Ok your updated answer worked great except I get two errors here: – user2909006 Oct 27 '13 at 20:10
  • ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager)); tvAppName.setText(entry.loadLabel(mPackManager)); Saying that it is undefined for the type PackageInfo – user2909006 Oct 27 '13 at 20:10
  • Ok nevermind fixed it. I posted the corrected code above. I'll let you know how it goes. – user2909006 Oct 27 '13 at 20:34
  • Ok so there's no errors but now how to I send the drawable bitmap to my gridView? Please note that in my adapter for the gridView I am using this process: drawables = new ArrayList(); and then drawables.add(drawable here) – user2909006 Oct 27 '13 at 20:39
  • Why are you using `ArrayList`? I can see it may have come from the fact that your resources such as `R.drawable.image` are ints, but drawables are different(http://stackoverflow.com/questions/11682130/anyway-to-compare-int-to-drawableandroid). You need to use an `ArrayList`. Then you can just add to the arraylist! – roarster Oct 28 '13 at 19:58
  • I did that because I was following a tutorial so that when the icon image pops up in the gridView, the user can drag and drop it to a trashcan where it will delete the icon from the gridView. I'll look into changing it though... – user2909006 Oct 28 '13 at 20:20
  • If they're just using those Integers as unique identifiers you should have loads of unique options in PackageInfo... – roarster Oct 29 '13 at 16:45
  • I believe that they are. What do you mean for the packageInfo part? Right now, all I am trying to do is the get the drawables I made in my onCheck method of my checkbox and then be able to add those to my gridView. I've started a new question [here](http://stackoverflow.com/questions/19669819/need-help-getting-drawable-from-one-class-into-gridview) so that we don't bog up this question. – user2909006 Oct 29 '13 at 21:47