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 Comments

  1. Pingback: GridView RandomFade animation in Android

  2. Carl January 28, 2014 at 1:01 am

    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.

     
    • James January 31, 2014 at 6:15 pm

      You can get the selected item ids in the callback.

       
  3. Bimal Thapa March 4, 2014 at 3:42 am

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

     
  4. George Thomas October 7, 2014 at 12:33 pm

    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

     
  5. Manoj October 28, 2014 at 10:01 am

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

     
  6. Subhadip Nandi December 19, 2014 at 4:51 pm

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

     
    • James December 19, 2014 at 5:46 pm

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

       
  7. Subhadip Nandi December 20, 2014 at 3:20 am

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

     
  8. Subhadip Nandi December 20, 2014 at 3:40 am

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

     
  9. Subhadip Nandi December 20, 2014 at 7:28 am

    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.

     
  10. Deepan September 19, 2017 at 6:54 am

    @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?

     

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.