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.

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:

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:

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:
&lt;RelativeLayout xmlns:android=""



Using this trick if we run the app we have:


  • Nice post.Give it up. Thanks for share this article. For more visit:Web App Development

  • Goran Ocokoljic

    Nice tutorial with some errors though. Thing you see as weird behaviour is actually an error in your code. The error is in onCheckedChanged function. You are acting as checkbox widget has it's own position in listView and that's not the fact. It is contained in relativeLayout that has position in listView and when you try to find position of checkbox, application hangs because you are trying to find something that doesn't exist.

    Solution is to create tag on checkbox with the position in getView function in adapter:


    and then you can get position of item in listView in the following way:

    int pos = (Integer)buttonView.getTag();

    instead of

    int pos = lv.getPositionForView(buttonView);

    Anyway, nice tutorial 🙂

  • Sagar Chavda

    Hello Admin,

    Can you please provide full source code of example. I had try to make it with your referd link but i face error.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.