RecyclerView Checkbox Android Example Get Checked Item Position

Warm welcome to RecyclerView Checkbox Android Studio example.

In this RecyclerView Checkbox Android example, we will cover single selection, multiple selection and also select all scenario.

There is a common problem like “checkbox unchecked when I scroll recyclerview.” We will solve this problem in this  RecyclerView Checkbox Android tutorial.

We will also send selected or checked items to next activity.

You can learn how to use onItemClickListener, onClickListener, onItemTouchListener etc. for recyclerview.

If you have not implement basic recyclerview with custom adapter yet, check recyclerview android example first.

Also checkout,

Now checkout the output of RecyclerView Checkbox Android example, then we will develop it.

Step 1: Create a new project in Android Studio.

Prepare a new project in android studio with empty activity as a default activity of your project.

Step 2: Updating build.gradle(Module:app) file

Add below source code into dependencies{}

Step 3: Coding cardview.xml

Create a new file into drawable folder and name it cardview.xml

Add below source code into it.

  • This file is used to create gradient effect with two different colors.
  • With help of this file, we will add some attractive design to our cardviews.
  • It will add this effect at the upper side of every card. You show this design in the above output video as well.

Step 4: Making rv_item.xml

Create a new layout resource file named rv_item.xml

Copy following source code into it

  • Above file is the common layout for each row of the recyclerview.
  • Adapter will create a view for every row item using this file.
  • I have taken one textview first. Here I have used it’s background design as a file named cardview.xml that we created in the previous step.
  • Now after that, I have taken one checkbox and one textview side by side (with horizontal orientation in LinearLayout)
  • textview represents the name of the animal.

Step 5: Making Model class

Create a new JAVA class named Model.java and paste below

Step 6: Adding integer.xml

Create a new file named integer.xml  in values directory and add below

Step 7: Code for CustomAdapter.java

Let’s create Custom Adapter for RecyclerView.

Make a new JAVA class named CustomAdapter.java and add below

Describing CustomAdapter

  • Above code will set a tag for position.

Now look at below source code

  • We will get position (which we set above using setTag() method) in onClick method.

Step 8: Making NextActivity

Create a new activity named “NextActivity

Add below source code into NextActivity.java

Copy following code into activity_next.xml

Step 9: Finally Update activity_main.xml and MainActivity.java

Updating activity_main.xml

Add following into activity_main.xml

Updating MainActivity.java

Copy folowing into MainActivity.java

Explaining Above

Look at the below line

  • Compiler will create a string array named animallist. It includes the names of the animals.

Consider the below code snippet

  • This line is preparing a arraylist to generate data.
  • Compiler is using getModel() method to have useful data.

Following is the code structure for getModel() method.

  • Compiler will run a for loop with four iterations.
  • In every iteration, compiler will create one object of the Model class.
  • Then it will set the name of the animal using string array (animallist). It will set the value of checkbox using isSelect boolean variable which is coming from the parameter.

Select All

We have taken a select all button in the activity_main.xml file.

Compiler will run the following code when the user clicks this button.

  • On the click of the button, compiler will generate new model arraylist with the help of getModel() method.
  • Here a parameter of getModel() method is set as a true. So data set will check all the checkboxes of the recyclerview rows.

Deselect All

  • I have taken a deselect all button in the activity_main.xml file.
  • System will execute the below code when user clicks this button.

  • Here, system will run the getModel() method with the parameter value as a false.
  • So all the checkboxes are deselected.

This was all for RecyclerView Checkbox Android tutorial. Thank you.

Download Source Code for RecyclerView Checkbox Android example

[sociallocker]Download RecyclerView_checkbox [/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

13 Responses

  1. Sankar says:

    I found your source code, actually my adBlocker was blocking it. superior logic, simple yet effective. Great work.

  2. vaibhav says:

    Thank uthis help me alot..

    pls help me to solve one more prob.
    when i change orientation my checkboxes in cardview get unchecked.

  3. Smita P says:

    Hi…

    May i Know how to store the values in firebase database with the help of Checkbox?

  4. Christy Harris says:

    Great tutorial, helped me a lot. +1 (if I could)

  5. Devil says:

    Where is The Source Code Link..??????

    • Hardik Parsania says:

      Soon after intoduction video.
      It is locked by default. Like Our facebook page to unlock download source code button. Cheers!

  6. Silva Lisya Ningsih says:

    how about insert output selected data into database mysql?? can you help??

    • Hardik Parsania says:

      For that you need to send data from Next activity.
      You also need one web service(REST API) which will send data from android device to mysql server.

  7. Hachich46Herma says:

    Ohh. Great tut ! Thanks a lot.
    You are a strong man !

Leave a Reply

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