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();
}