Kotlin ListView CheckBox Android Tutorial Example Get Checked Item

Kotlin Listview CheckBox Tutorial with Example is the matter here.

It is not easy to implement the listview with checkbox in it’s every row item especially in the kotlin language.

This tutorial will provide easy to learn and time saving source code.

This example includes the features like select all, deselect all, make multiple selection etc.

We will learn how to get click position on checkbox, get the names of checked items and send it to the next activity.

If you are not familiar with kotlin then visit java version here.

Read Other Kotlin Listview Tutorials

Refer the output of kotlin listview checkbox example in below video.

Download source code for kotlin listview checkbox example

Step 1. Model Class

Create a new project in the android studio with kotlin support.

Select the default activity as empty activity in your last step.

Now, make a new kotlin class. Give this class a sweet name like Model.kt

Source code for the model class is

Step 3. Listview Layout File

We need to create a layout resource file which will represent the every row’s layout.

Create new layout resource file named lv_item.xml and add below code

Step 4. Adding Custom Adapter

Before making adapter, create a new xml file named integer.xml in res/values directory(same directory in which string.xml is there)

Now add following code into integer.xml

Prepare a new class CustomAdapter.kt

Copy and paste below source code into it

setTag() and getTag() methods are used to take care of each checkboxes in listview.

Description of setTag() method

this method is used to set a key and value pair to each view of the listview.

Look at the below lines from the Adapter class

In the first line, converView is set as a value and R.integer.btnplusview is set as a key.

Second line says that the current position is set as a tag.

One important point is that we have make one public arraylist named public_modelArrayList

This arraylist is the list of the objects of the Model class.

Following line declares this public arraylist.

This public arraylist is updated when the checkbox is clicked everytime.

Step 5. Making Next Activity

Let us create a next activity in which, we will send the selected checkbox’s values.

Create a new activity named NextActivity.

Add below code into NextActivity.kt class

We are printing the selected checkbox’s values in TextView.

Source code for activity_next.xml is as follows

Step 6. Update Main Activity

Make your MainActivity.kt class looks like below

Logic for select all and deselect all features are written in the above class.

Write below code into activity_main.xml

This was all the details about making a listview with checkbox in android with kotlin language.

Comment out your queries, we will answer you as soon as possible.

Do not forget to Share this resource in your social network.

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

Leave a Reply

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