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:

 

Download Source Code Listview with Checkbox in Android Studio

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 following source code in it:

Step 3. Creating lv_item.xml layout file

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

Step 4. Updating activity_main.xml

Copy and Paste following source code in activity_main.xml

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

Copy and paste following code in this class:

Now check following code snippet:

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

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

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

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:

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!  

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 *