Android Custom ListView :

By default, the ListView allows one item to be chosen. To enable multiple items to be chosen, you’ll configure the ListView to display a checkbox next to each item. You can combine many components like TextView , CheckBox, ImageView, ImageButton etc. and can make Listview as you require.

You have already learned how to use Simple ListView in old post “Displaying a List of Items Using the ListView in Android“. This Example shows you how to add Checkbox in a ListView. We will develop custom array adapter to populate our android Listview with Checkbox.

Custom listview checkbox

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 22.6.3

Solution Stuff :

To configure the ListView, you initially need to get a reference to the ListView. If your activity is extending the ListActivity base class, you’ll use the getListView() method to return an instance of the ListView in your activity. Once you get an instance of the ListView, you’ll call the setChoiceMode() method to set the display mode of the ListView:

The ListView.CHOICE_MODE_MULTIPLE constant displays a checkbox next to each item within the ListView. as the user selects each item, the onListItemClick() method is called.

See figure Multiple choice in listview :

android Custom listview checkbox To determine whether an item within the ListView is checked or unchecked, you’ll use the second argument of the onListItemClick() method:

The second argument returns the view of the item that was chosen in the ListView. in this case, it returns a CheckedTextView object. using this object, you’ll check whether it’s been checked or not using its isChecked() method.
Another nice feature of the ListView is its support for text filtering. If your device has a hardware keyboard, typing in the names of the {items|the things} in your ListView can automatically cause the ListView to display only those items that match the text you enter. To enable filtering on the ListView, call the setTextFilterEnabled() method:

Figure shows what the ListView will look like when you type the characters “ B ”.

Custom listview checkbox example

Complete Code :

1. Open “res/layout/activity_main.xml” file and put this xml code it into file:

File : res/layout/activity_main.xml

2. Open “MainAcitvity.class” file and add following JAVA code.

File : src/package-name/MainActivity.java

Output of  Custom Android ListView with CheckBok Android emulator :

Custom listview checkbox exmple

Download Android Custom ListView with CheckBox example Source code :

  1. Quite useful!
    Is this specifically for kitkat version and above? I’m looking for something similar for targetSdkVersion=”17″ and above.

  2. R.K.Kanojia (Android App Developer)

    No its not specifically for kitkat, it will be work on all versions of android apk, it only tested on Kitkat. Thanks..

  3. Thanks You…I have one query do i have to change the tick color or is it by default..i am new to android..Just started.Any help would be appreciated. Thank again for simple tutorial

  4. Can You give Some idea to do custom click..Just a brief explantion would be very helpful..Because a trying to do custom click..there is no other example in google .This is the first example with simple click(tick )
    i want to do on click it shows click button and again on click tick will disappear.Thank You

    • R.K.Kanojia (Android App Developer)

      Hi, you want custom checkbox (tick) in ListView? if yes then you want to create custom ListView xml and add in code
      =============================
      setListAdapter(new ArrayAdapter(this,
      android.R.layout.simple_list_item_checked,city));
      }
      =====================================
      (R.layout.custom_list)
      custom_list.xml should be your define list view(with tick checkbox)

  5. Hi

    How do you add an image to this solution when using CHOICE_MODE_MULTIPLE and therefore not creating a separate xml file for the layout (where you normally add the imageview)
    In my case I just need an icon on every line in the listview.

Leave a Reply