RecyclerView with CheckBoxes – Android


In this article, we are going to see how we can add CheckBoxes in RecyclerView. After reading this article you will also have a clear understanding of how RecyclerView works.
Before going any further I hope you guys know what RecyclerView is or have worked with RecyclerView in your projects at least once.
At first, I am going to show you what makes this topic so special.

  • Let’s create a new project and name it “RecyclerView With CheckBox“.

Add gradle dependencies to your app’s module.

Or you can add it from project structure.Go to File – Project Structure – Select ‘app’ under modules – Dependencies – Click the ‘Add’ icon – Select Library dependencies – type ‘recyclerview‘ in the search box and select ‘recyclerview-v7‘.

After adding the dependency we can add the RecyclerView widget to our XML layout. Set its height and width as match_parent.

We also need a layout for our RecyclerView row, which will contain a simple TextView and a  CheckBox. Create an XML layout for the rows and name it as ‘custom_row.xml’.

Create a Getter and Setter class and name it as ‘‘. We need a String variable here to populate the TextViews. So declare a variable ‘String text;‘.
To auto-generate getter and setter method, press Alt+Insert and select Getter and Setters.

Now let’s make the adapter for our RecycleView. Create a different package and name the package as ‘Adapters’. Under Adapter package create the adapter class and name it as ‘RvCheckBoxAdapter‘.


Now we are going to populate the RecyclerView from our class.

Now run your app. Check the first CheckBox and scroll down. Did you notice something? RecyclerView keeps on checking different CheckBoxes. Here comes the recycling concept. The RecyclerView is recycling the views and using them again and again. This is the main advantage of RecyclerView. But in our program, we want only the clicked CheckBoxes to be checked. So to achieve this we can do the following steps –  

Declare a boolean variable in Getter Setter class. In our case its ‘‘.

Go to your adapter class and in onBindViewHolder make these changes:

Notice the getAdapterPosition. If you don’t know how it works. read here.

Now run the app and you will notice the difference.

Our final code:


Hope you understood how to work with RecyclerView when using CheckBoxes. If you have any doubt or query related to this post, please do comment below in the comment section.


You May Also Like

1 Comment

Leave a Reply

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