bricklore
bricklore

Reputation: 4165

dialog with all installed applications

How can I create a dialog showing all installed apps in a scrollview with their icons on the left side?

Dialog d = new Dialog(context);
LayoutInflater l = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = l.inflate(R.layout.my_list_dialog_layout, null, false);
d.setContentView(v);
d.show();

something like this would work for a static layout, but how can i create the list with all installed apps programmatically (at runtime)?

»»»»»»» UPDATE «««««««

as some people didn't got what i wanted, here explained further:
i don't want to know how to get all installed packages
i want to know how to create a listview in a dialog at runtime?
i'm sorry that i asked my question so inaccurate...

Upvotes: 0

Views: 1005

Answers (1)

SilentKiller
SilentKiller

Reputation: 6942

Check the following code :

class AppInfo {
    private String appname = "";
    private String pname = "";
    private String versionName = "";
    private int versionCode = 0;
    private Drawable icon;
    private void prettyPrint() {
        Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
    }
}

private ArrayList<AppInfo> getPackages() {
    // false = no system packages
    ArrayList<AppInfo> apps = getInstalledApps(false); 

    final int max = apps.size();
    for (int i=0; i<max; i++) {
        apps.get(i).prettyPrint();
    }
    return apps;
}

private ArrayList<AppInfo> getInstalledApps(boolean getSysPackages) {
    ArrayList<AppInfo> res = new ArrayList<AppInfo>();        
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    for(int i=0;i<packs.size();i++) {
        PackageInfo p = packs.get(i);
        if ((!getSysPackages) && (p.versionName == null)) {
            continue ;
        }
        AppInfo newInfo = new AppInfo();
        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
        newInfo.pname = p.packageName;
        newInfo.versionName = p.versionName;
        newInfo.versionCode = p.versionCode;
        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
        res.add(newInfo);
    }
    return res; 
}

Dialog Code :-

Dialog d = new Dialog(context);
LayoutInflater l = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = l.inflate(R.layout.my_list_dialog_layout, null, false);
ListView mApplicationList = (ListView) v.findViewById(R.id.list);
ArrayList<AppInfo> apps = getPackages();
ApplicationAdapter listadaptor = new ApplicationAdapter(AllAppsActivity.this,
                R.layout.snippet_list_row, apps);
setListAdapter(listadaptor);
d.setContentView(v);
d.show();

Adapter Code :-

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;

public ApplicationAdapter(Context context, int textViewResourceId,
        List<ApplicationInfo> appsList) {
    super(context, textViewResourceId, appsList);
    this.context = context;
    this.appsList = appsList;
    packageManager = context.getPackageManager();
}

@Override
public int getCount() {
    return ((null != appsList) ? appsList.size() : 0);
}

@Override
public ApplicationInfo getItem(int position) {
    return ((null != appsList) ? appsList.get(position) : null);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.snippet_list_row, null);
    }

    ApplicationInfo data = appsList.get(position);
    if (null != data) {
        TextView appName = (TextView) view.findViewById(R.id.app_name);
        TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
        ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

        appName.setText(data.loadLabel(packageManager));
        packageName.setText(data.packageName);
        iconview.setImageDrawable(data.loadIcon(packageManager));
    }
    return view;
}
};

- Another Sample link

Upvotes: 4

Related Questions