Listview With Checkbox In Android Studio Example

In Listview with Checkbox in Android Studio example,learn how to make custom Listview with a checkbox.

You will have to create a listview with checkbox in Android Studio when you want to show certain options to the user and wants to take more than one options to be selected.

There is a common problem like “checkbox unchecked when I scroll listview.”

We will solve this problem in this listview with checkbox in Android Studio.

We will use listview’s onitemclicklistener() and checkbox’s OnClickListener() method to get checked items.

In this example, we will get single selection, multiple selection and also select all scenario.

Check out the output of Listview with Checkbox in Android Studio tutorial below which is prepared by Demonuts.com:

 

If you have not any basic idea about listview and custom adapter, then it is better to learn about them first and then you will find this tutorial easy to understand. Follow this example first about basic listview.

Step 1.  Create a new project in the Android Studio.

Choose empty as your Main Activity.

Step 2. Create a class named Model.java

copy the following source code in it:

This class represents the various methods to set and get the data in the adapter class.

Objects of this class will be used to maintain the proper data structure.

Step 3. Creating lv_item.xml layout file

Create a new layout resource file named lv_item.xml file and add following

This file will create the layout for all the rows of the listview. Adapter class will use this file.

It contains one checkbox and one textview.

Step 4. Updating activity_main.xml

Copy and Paste following source code in activity_main.xml

I have taken one listview and three buttons in main layout.

Step 5. Create one class and name it: CustomAdapter.java

Copy and paste following code in this class:

We are getting one arraylist of the objects of the Model class in the constructor.

We are using this arraylist in the below method.

Now check following code snippet:

Check first two lines of above snippet. Two tags are set to checkbox

  • One for getting position (checkbox.setTag())
  • And other for getting whole row view in which checkbox is present. (checkbox.getTag())

To set the Tag of view, we need to assign key which is R.integer.btnplusview here.

  • When we are getting this view using getTag, we used the same key to get the whole view as shown in the checkbox.OnClickListener().
  • Now you might say what is R.integer.btnplusview??
  • It is nothing but same as you define your string resources in the string.xml, you can define your integer resources in an integer.xml file.
  • When you create a project in the android studio, string.xml is generated automatically, while you have to manually create integer.xml , that’s it, nothing special.

Create integer.xml under res/values directory(same directory in which string.xml is present) and copy following code:

  • The Logic for updating value of each model item is written on OnClickListener() of  the checkbox.
  • We have used setTag and getTag to get which checkbox is clicked(position of clicked checkbox), and we can also get the whole view of row item which is clicked, and it is taken as tempview in above code.
  • Using tempview, you can inflate all views(TextView, EditText,ImageView etc.) of that particular row(row which is clicked).

TextView is inflated from tempview, and then we can get the value of this textview to use as per requirements.

Step 6. Copy and paste following code in MainActivity.java

There are three buttons in the above code snippet.

Below is the onclick for first one

When the user clicks on this button, all the checkboxes in the listview are automatically selected or checked.

Next button will take the user to the Next Activity.

Consider below code

When the user clicks the deselect button, all the checkboxes in the listview are deselected.

This select all and deselect all feature is very useful for user when he wants to do all the process from scratch.

Look at the below method

getModel() method will create an arraylist of the objects of the model class.

This arraylist is sent to the adapter class via constructor where it will work as the data provider.

Step 7. Create a new activity and name it NextActivity.

Now when clicking on Next button, a new activity opens with names of selected animals as shown in the output video.

So you will have two files: 1. NextActivity.java and 2. activity_next.xml

Copy and paste following code into NextActivity.java class:

for loop in the above code will check all the object one by one. It will print the animal name which are checked.

Copy following in activity_next.xml

Observe all the code snippets for listview with checkbox example carefully and understand it throughout so that you can change all things as per your different requirements.

All for Listview with Checkbox in Android Studio. Cheers and happy coding!  

Download Source Code Listview with Checkbox in Android Studio

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...

16 Responses

  1. damodar says:

    im not able to copy this code please provide source code

  2. mm says:

    I want to add an image to the viewpager instead of a textview for NextActivity. What should I do?

  3. Nensee Kaneriya says:

    thanks for the nice code
    but i want selected data in listview on nextpage, can you help me?

  4. Patrick says:

    Nice code, I understand most of the code.
    I would like when i click on one or more of the checkboxes retrieve the total of checkboxes checked on the activity_main.xml
    in a field like total on the top of the listview
    Like the Toast but put the output on a textview in the activity_main.xml
    in a field named total.
    I dont understand how to do that.
    Any help is welcome

    Patrick

  5. priscilla says:

    I would like to add the checkbox list as equipment checklist in my app and store it in firebase. Would be great if you can do a tutorial regarding it.

  6. priscilla says:

    I would like to add the checkbox list as equipment checklist in my app and store it in firebase. Can you please do a tutorial regarding it.

  7. chakry says:

    i am using checkbox in the custom list and setting that custom list to adapter but my app is getting crashed how can i know the reason

  8. Kashif Qasim says:

    I want to store checked items in fire base database of list item.

    • Hardik Parsania says:

      You are getting checked item in the next activity, send checked items to firebase from this activity

Leave a Reply

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