0

I am working on a custom launcher for android. I need to take a ArrayAdapter to feed into my GridView, which lists the apps. My current code is as follows:

public class LauncherActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launcher);
    final PackageManager pm = this.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    //need to define the arrayadapter here

    GridView gv = (GridView) findViewById(R.integer.gv_id);
    WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());
    getWindow().getDecorView().findViewById(android.R.id.content)
    .setBackground(wm.getDrawable());
    //gv.setAdapter(adapter);

 }
}

How do I create an ArrayAdapter with all the apps in Android?

figgyc
  • 302
  • 2
  • 5
  • 16

1 Answers1

0

take a look this code:

Here how you can recieve list of programs:

public class ProgramListBuilder {
    private static final String TAG = "ProgramListBuilder";
    private Context context;

    public ProgramListBuilder(Context context) {
        this.context = context;
    }
    public List<Program> buildProgramList() {
        List<Program> programs = new ArrayList<Program>();
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PackageManager packageManager = context.getPackageManager();
        final List<ResolveInfo> pkgAppsList = packageManager.queryIntentActivities(mainIntent, 0);
        for (ResolveInfo ri : pkgAppsList) {
            Program p = new Program((String) ri.loadLabel(packageManager), ri.loadIcon(packageManager));
            programs.add(p);
        }
        return programs;
    }
}

Where Program is:

public class Program {
    private Drawable icon;
    private String name;

    public Program(String name, Drawable icon) {
        this.name = name;
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public Drawable getIcon() {
        return icon;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }
}

So Adapter can be like this:

public class ProgramListAdapter extends ArrayAdapter<Program> {

    public ProgramListAdapter(Context context, int resource, List<Program> programs) {
        super(context, resource, programs);
    }

    static class ViewHolder {
        ImageView icon;
        TextView name;
    }

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.program_row, parent, false);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.program_name);
            holder.icon = (ImageView) convertView.findViewById(R.id.program_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Program p = getItem(position);

        holder.name.setText(p.getName());
        holder.icon.setImageDrawable(p.getIcon());

        return convertView;
    }

}

UPD: program_row here is:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/program_icon"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:padding="1dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/program_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"
            android:text="My Program Name"
            android:layout_marginLeft="2dp"/>


    </LinearLayout>

</LinearLayout>

USAGE:

ListView listView = (ListView) findViewById(R.id.program_list);
ProgramListBuilder programBuilder = new ProgramListBuilder(this);
listView.setAdapter(new ProgramListAdapter(this, R.layout.program_row, programBuilder.buildProgramList()));

Let me know if something is hard to understand here.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • There's an error in the List at public List buildProgramList() { and it mentions near the bracket an expression and ; are expected – figgyc Apr 13 '14 at 17:22
  • Oh I fixed the above by putting List outside OnCreate. – figgyc Apr 13 '14 at 17:35
  • I'm a bit confused how to retrieve the ProgramListAdapter. I tried gv.setAdapter(new ProgramListAdapter(getApplicationContext(), R.integer.gv_id, buildProgramList())); where gv_id is the ID of the gridview, but that does not work. – figgyc Apr 13 '14 at 17:42