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:

 

How to get maximum benefit from Listview with Checkbox in Android Studio tutorial

Download Source Code

Try Our App

Download DemoNuts app to learn anytime, anywhere: Click to get Demonuts App

Or search “demonuts” in play store from android mobile device.

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.

Create Listview with Checkbox in Android Studio

If you are using adblocker, then you will not be able to show our site smoothly. For example, you find step 6 after step 1 or similar to this. So please disable adblocker to fully utilize all resources of DemoNuts

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

Step 2. Create a class named Model.java and 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. 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.

Google search tip

Click to get Important Google searching tips

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

6 Comments
  1. damodar
    • Hardik Parsania
      • mani
  2. mm
    • unknown
  3. Nensee Kaneriya

Leave a Reply

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