This tutorial shows you how to add checkboxes to a listview in an Android app. We will develop a custom array adapter to populate our Android listview with checkboxes.

The listview contains data in multiple rows and we need to insert each row separately as each row has  its own checkbox and a textbox. We can accomplish this by implementing our own custom adapter.

Desired Output:


Steps involved in adding checkboxes to the listview.


We need two XML files to add checkboxes to a listview. The first XML file will hold the listview and the second XML file will contain the row items (checkbox and a textview).

Designing a row

The listview is made up of multiple rows so we need to design our rows. Below is the simple XML file which contains a checkbox and a textview.

Listing 1 : Create a new file row.xml

Next we need a XML file to hold the listview. 

Listing 2 : Create a new file Activity_main.xml

Custom Array Adapter

Our goal is to add data inside the listview with a checkbox. The listview is simply a list of view items. Now to place data inside the listview, we need to convert the data items into a view item. Below is the pictorial representation of a working custom adapter.




Data Item

We want to place a data item inside the listview. Each data item will have a name and a value which tells wheather the item is selected or not. So, create a data model class. We will create an array of data items and pass it to the adapter. The adapter will convert these data items into view items.

Listing 3 : Create a new file

Developing a Custom Adapter to convert data items into view items.

Listing 4 : Create a new file


  1. To implement a custom adapter, we need to create a sub class of the ArrayAdapter.
    Snippet 1
  2. modelItems holds the array of data items.
    Snippet 2
  3. Context holds a reference to an activity which is using the custom adapter.
    Snippet 3
  4. The Constructor for the CustomAdapter helps to initialize the data items and the context.
    Snippet 4
  5. The getView() method is used to build the view item. This method returns a view item which is placed as a row inside the listview.
  6. Get the layout inflater.
    Snippet 5

  7. Inflate the layout with the row (checkbox and textview).
    Snippet 6
  8. Get the reference of textview and the checkbox
    Snippet 7

  9. If the value is 1 (true), the item is selected. Set the value of the checkbox.
    Snippet 8
  10. Return the view item.
    Snippet 9

Build the main activity for the listview with checkboxes.

In the MainActivity, we will create a reference of the listview and will link it with the XML file. Then we will pass data items to the constructor of the custom adapter. After that, we will set the adapter on our listview.

Listing 5 : Create a new file

We learned about the Android listview with checkboxes. If you have any suggestions or queries, feel free to post.