Listview with Checkbox – Custom Adapter

This tutorial describes how to use Android ListView with checkbox. This is an important component and it is widely used and has many applications in different fields. In this post, i want to show how to use ListView with Checkbox so that user can select items and give you some tricks to handle some problems.

android listview with checkbox

We want to implement an Android ListView with checkbox where each item has a checkbox like the figures shown below:


At the same time we want to handle user click so that we keep track of items checked and give access to the item menu.
Let’s start.

Implement a layout of Android Listview with checkbox

The first thing we have to do is implementing the layout like the the one shown below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

<CheckBox android:id="@+id/chk"



This layout implements the structure shown in the pictures above.
<h2 class="Paragrafo">The Adapter and User Events</h2>
Once we have our layout, we use it in our Adapter. As discussed in other post (<a href="" target="_blank"><strong>Android ListView : Custom Adapter And Layout</strong></a> and <a href="" target="_blank"><strong>Android ListView : ArrayAdapter, Manage Items, User Interaction</strong></a>), we have to modify our <strong>getView</strong> method so that we can handle the checkbox. The code is very similar to others used in other post, we simply need to get the reference to the Checkbox component and handle it.
[java]CheckBox chk = (CheckBox) v.findViewById(;
holder.chk = chk;
chk.setOnCheckedChangeListener( (MainActivity) context);[/java]
In the last line we simply define the listener invoked when user clicks on the <strong>Checkbox</strong>. Java purists can discus if it is better to implements the listener directly inside the adapter or somewhere else (MainActivity in our case). Well in my vision the adapter should be used just for filling the <strong>ListView</strong>, so that <strong>MainActivity</strong> class is my opinion should the one that handles user touch interactions.

The MainActivity becomes:
[java]public class MainActivity extends Activity implements android.widget.CompoundButton.OnCheckedChangeListener {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv.getPositionForView(buttonView);
System.out.println("Pos ["+pos+"]");
if (pos != ListView.INVALID_POSITION) {
Planet p = planetsList.get(pos);

The <strong>onCheckedChanged</strong> method is invoked when user clicks on the checkbox. In this method we handle the event, finding first the position or in other words the item position inside the ListView. Then we verify that this position is valid, comparing it against <strong>ListView.INVALID_POSITION</strong> and if everything is ok we set the boolean attribute of the Planet class the the isChecked parameter value.

Running the app you have this result:

<a href=""><img style="background-image: none; display: block; float: none; margin-left: auto; margin-right: auto; padding-left: 0px; padding-right: 0px; padding-top: 0px; border-width: 0px;" title="android_listview_with_checkbox_item" src="" alt="android_listview_with_checkbox_item" width="275" height="480" border="0" /></a>
<h2 class="Paragrafo">Analysis of some “WIRED” behaviours</h2>
If you “play” with the app you could notice some strange behaviours. It crashes!!!…Yes right. It happens that in some circumstances it suddenly crashes as you check the checkbox. Why?! Well this is a mystery in my opinion. If you read the log you get a NullPointerException in <strong>getPositionView</strong>. It could be because reuse the View in the <strong>ListView</strong> to optimize the resources. Anyway, investigating the problem it seems to me that the check listener is attached to an invalid checkbox that i suppose it is caused by the View re-use. The solution (but i don’t know if it is correct) is the “refresh” the listener reference everytime we get a item view reference. So in our adapter we have to modify a bit the code:
[java]//chk.setOnCheckedChangeListener( (MainActivity) context); // We have to remove it it causes NPE


holder.chk.setOnCheckedChangeListener( (MainActivity) context);
Another “strange” thing, maybe you already noticed, it is the fact we can’t select the item anymore so if you long press on the item the menu doesn’t appear anymore. There’s a solution explained in this <a href="" target="_blank">link</a>, this behaviour is caused by components that “locks” the focus. If you read the solution you can find that it is enough blocking focus on descendants, like that:
[xml]&lt;RelativeLayout xmlns:android=""



Using this trick if we run the app we have:



  1. imran khan March 19, 2013
  2. Goran Ocokoljic November 25, 2013
  3. Sagar Chavda May 26, 2014

Add Comment