Android ListView Checkbox - Androhub

CheckBox List Banner

Android ListView Checkbox

Hi to all, today we are going to learn how to enable multiple items to be chosen in ListView, GridView and in RecyclerView using CheckBox, you’ll configure the ListView to display a checkbox next to each item. You can combine many components like TextView, CheckBox, ImageView, etc. and can make ListView as you require.

Prerequisite:

  1. Material Tabs Tutorial
  2. ListView Tutorial
  3. GridView Tutorial
  4. RecyclerView Tutorial

Example

In this tutorial, we are going to learn how to enable multiple items to be chosen in ListView, GridView and in RecyclerView using CheckBox.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open build.gradle and include this libraries show below:

3. Now create 3 material tabs naming ListView, GridView and RecyclerView. For this you can check this link for reference.

4. After creating tabs lets move to creating xml layouts for individual tabs.

  • list_view_frament.xml

  • grid_view_fragment.xml

  • recycler_view_fragment.xml

5. In all the above xml layouts i am using one common layout i.e. bottom_container.xml. Lets create the common layout xml, this layout contains three buttons for implementing actions over selected rows.

6. Now lets create java classes for all the above three xml files.

  • ListViewFragment.java

  • GridViewFragment.java

  • RecyclerViewFragment.java

In all the three above Java classes the main thing that we need to focus is button click events:

  • Show Button : This button used to show a Toast of checked rows.

  • Delete Button : This button is to delete checked rows.

  • Select All/Deselect All Button : This button is to select and deselect all the rows.

7. Now after creating all fragment classes we have to create Adapters for ListView, GridView and RecyclerView.

  • GridListAdapter.java

  • RecyclerViewAdapter.java

In both the above adapter the common code is to select rows:

8. Finally all things done. Now you can also make your any type of multiple choice list view.

Thanks. 🙂

 

13 Comments

nadia
Thursday, February 1st, 2018

Hi nice work
i have the same work to do ,
me i used database SQLITE , so i have table product(id_product, name_product, image_product, namecategory) so in my first interface “fragment” i choos my category with image_category “the image it will be stock in drawable” and after when i click in one category like fruit so one spinner”listProduct” it will be open with the list of fruit i can do multi check for some product i want and i valide so when i valide it will stock in table and show in another fragment name “list”. So my problem that what is the solution should used after valide to return choose another category like vegetables and i click in image so interface “listProduct” it will be open again but with list of vegetables i check what i want like potato,peas and i valide so this list of Vegetables it will be add to list of fruits and when i click show list i can see the finally list , How can do it ?? The greate solution hope can help me thanks

Néstor
Thursday, July 12th, 2018

How to catch the Item Click inside Fragment?

Dr. Droid
Friday, July 13th, 2018

Hi Nestor,

Item click inside Fragment will be implemented same like we do in Activity.

Thanks

Néstor
Friday, July 13th, 2018

Good, I am just trying to do it but seems not work. The setOnItemClickListener is not firing.

Dr. Droid
Saturday, July 14th, 2018

Hi Nestor,

Can you share your code that you are trying to do at my Email Id : [email protected].

Thanks

Obie
Thursday, October 25th, 2018

Hi, I’ve got issue .. i’ve implement your code into my code..
In my code, assume I have database List 100 array data in my checkbox list [0, 1, 2, 3, 4, 5……100]. after that I am check it in [2,4,5].. when debug I am just get value [2,4] .. it mean I didn’t get value of 5. can you help me please?\
thank you

Dr. Droid
Thursday, October 25th, 2018

Hi Obie,

Can you check your code that 5th number item is checked or not? And if checked use some other cases like checking more items and debug and check if you are getting checked ones or not?

If still the issue is there then mail me your code/classes ay mail id. You can find it on the top of the blog.

Thanks

LyonOconner
Monday, January 28th, 2019

there has to be a easier way, rigth ? is many code for simple checkbox in a gridview :c

Dr. Droid
Tuesday, January 29th, 2019

Hi Lyon,

The code contains Listview, gridview and recyclerview example. May be due to that you feeling it’s too many code.
And also for listview and gridview I am using same code.

Thanks

Lyon
Monday, January 28th, 2019

You can update this sample at Android 3.3 pls ????

Dr. Droid
Tuesday, January 29th, 2019

Hi Lyon,

What issue you are facing in current code?

Thanks

Charlotte
Thursday, September 10th, 2020

hello, can we put a checkbox in a cardview from a recyclerView ? I need to save items selected (name of room) from a postgresql and i have made a cardview. Can i make a filter of this witch checkbox ? thank you

Dr. Droid
Wednesday, September 16th, 2020

Hi Charlotte,

Yes, it’s possible and you can do it, Just need to add extra filter logic.

Thanks

Post comment

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