Multiple Selection GridView in Android

By | April 26, 2013

This is a simple post that helps you to do multiple selection in a ListView.

Check out previous posts for 3D animation in a Listview.

At first we will see the XML layout file.
This layout contains a gridview since we are dealing with it only.

Multiple Selection GridView

Multiple Selection GridView

grid_1.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="60dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:padding="10dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

Now the MainActivity that does all the work for the multiple selection in a ListView.

package com.coderzheaven.multipleselectiongrid;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {

	GridView mGrid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		loadApps();

		setContentView(R.layout.grid_1);
		mGrid = (GridView) findViewById(R.id.myGrid);
		mGrid.setAdapter(new AppsAdapter());
		mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
		mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener());
	}

	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) {
			CheckableLayout l;
			ImageView i;

			if (convertView == null) {
				i = new ImageView(MainActivity.this);
				i.setScaleType(ImageView.ScaleType.FIT_CENTER);
				i.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
				l = new CheckableLayout(MainActivity.this);
				l.setLayoutParams(new GridView.LayoutParams(
						GridView.LayoutParams.WRAP_CONTENT,
						GridView.LayoutParams.WRAP_CONTENT));
				l.addView(i);
			} else {
				l = (CheckableLayout) convertView;
				i = (ImageView) l.getChildAt(0);
			}

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

			return l;
		}

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

	public class CheckableLayout extends FrameLayout implements Checkable {
		private boolean mChecked;

		public CheckableLayout(Context context) {
			super(context);
		}

		@SuppressWarnings("deprecation")
		public void setChecked(boolean checked) {
			mChecked = checked;
			setBackgroundDrawable(checked ? getResources().getDrawable(
					R.drawable.blue) : null);
		}

		public boolean isChecked() {
			return mChecked;
		}

		public void toggle() {
			setChecked(!mChecked);
		}

	}

	public class MultiChoiceModeListener implements
			GridView.MultiChoiceModeListener {
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			mode.setTitle("Select Items");
			mode.setSubtitle("One item selected");
			return true;
		}

		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return true;
		}

		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			return true;
		}

		public void onDestroyActionMode(ActionMode mode) {
		}

		public void onItemCheckedStateChanged(ActionMode mode, int position,
				long id, boolean checked) {
			int selectCount = mGrid.getCheckedItemCount();
			switch (selectCount) {
			case 1:
				mode.setSubtitle("One item selected");
				break;
			default:
				mode.setSubtitle("" + selectCount + " items selected");
				break;
			}
		}

	}
}

You don’t have have to change anything in the manifest for this to work.
Now go on and run it.

You can download the complete sample code for the above post from here.

Join the Forum discussion on this post

Join the Forum discussion on this post

13 thoughts on “Multiple Selection GridView in Android

  1. Pingback: GridView RandomFade animation in Android

  2. Carl

    How would I be able to send the selected items (in this case those apps) to another activity and keep them saved there? Let’s say into a listview in my fragment.

    Reply
  3. Bimal Thapa

    How to select imageview of video using this project?
    Video thumnails are showing but the background when selected is not shown.

    Reply
  4. George Thomas

    Hi ,

    I implemented you code its working fine,it shows the selected blue image under the image , can u tell me how to show the selection above the image which needs to be selected

    Reply
  5. Manoj

    i need to carry those selected items to another activity .
    how can i do that

    Reply
  6. Subhadip Nandi

    Where are the images stored? Could not find them in drawable folder.

    Reply
    1. James Post author

      These are the apps installed in your device and its icon. there is nothing in the resources of the project.

      Reply
  7. Subhadip Nandi

    Can you please tell me how to modify the code so that it works for images on my drawable folder?

    Reply
  8. Subhadip Nandi

    Never mind. I got it. Thanks a lot, this is a very useful resource, helped me a lot.

    Reply
  9. Subhadip Nandi

    One problem however is that setchoicemode() is not recognized by android versions lower that icecream sandwitch. Is there a way to overcome this? Pls help.

    Reply
  10. Deepan

    @SuppressWarnings(“deprecation”)
    public void setChecked(boolean checked) {
    mChecked = checked;
    setBackgroundDrawable(checked ? getResources().getDrawable(
    R.drawable.blue) : null);
    }

    the above set of code(suppressWarnings deprection) it’s works in android nougat but not in any lower version of android. What is wrong with it?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *