How do I launch an app from an icon in a GridView?

I am trying to create a launcher that I have added GridView

. This fetches all installed apps and arranges them in a grid.

The problem is that when I click on these grid items nothing happens, I want it to open the application.

Is it possible? Here's my code:

Apps.java:

public class Apps extends Activity {

    SharedPreferences colors_app;
    GridView mGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        loadApps();

        setContentView(R.layout.apps);

        mGrid = (GridView) findViewById(R.id.app_grid);
        mGrid.setAdapter(new AppsAdapter());

        colors_app = getSharedPreferences("MyColor", 1);

        int colorcode2 = colors_app.getInt("color_code", 0);
        if (colorcode2 != 0) {

            Apps.this.findViewById(R.id.apps_back).setBackgroundColor(
                    colorcode2);

        }
    }

    private List<ResolveInfo> mApps;

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

        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
    }

    public class AppsAdapter extends BaseAdapter {
        public AppsAdapter() {
        }

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

            if (convertView == null) {
                i = new ImageView(Apps.this);
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                i.setLayoutParams(new GridView.LayoutParams(90, 90));
                i.setPadding(10, 10, 10, 10);
            } else {
                i = (ImageView) convertView;
            }

            ResolveInfo info = mApps.get(position);
            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

            return i;
        }

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

      

+3


source to share


2 answers


There is nothing in your code that should have launched another application. You need to add code to launch the apps displayed in your grid. You can implement OnItemClickListener

for mGrid

or add OnClickListener

for each ImageView

inside the adapter.

Check out this example from The Early Nerd Ranch Guide :



@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    ResolveInfo resolveInfo = (ResolveInfo)l.getAdapter().getItem(position);
    ActivityInfo activityInfo = resolveInfo.activityInfo;
    if (activityInfo == null) return;
    Intent i = new Intent(Intent.ACTION_MAIN);
    i.setClassName(activityInfo.applicationInfo.packageName, activityInfo.name);
    startActivity(i);
}

      

Remember, this code should work with ListView

, not with GridView

, so be sure to use something like this for your case. You already have a list of objects ResolveInfo

, so you can use it to access ActivityInfo

each application through the position of each item.

+2


source


I found the answer myself :-) Just put this code in onCreate

below setContentView

and you're done! The code works like a charm!



mGrid.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View convertView,
                    int position, long id) {
                ResolveInfo cleckedResolveInfo = (ResolveInfo) parent
                        .getItemAtPosition(position);
                ActivityInfo clickedActivityInfo = cleckedResolveInfo.activityInfo;

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.setClassName(
                        clickedActivityInfo.applicationInfo.packageName,
                        clickedActivityInfo.name);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                startActivity(intent);
            }

        });

      

0


source







All Articles