Android ExpandableListView Checkbox Example Handle Multiple Selection

Android ExpandableListView Checkbox Example is covered in this post.

We will put checkboxes in the child rows of expandable listview.

We will solve the general problems like

  1. checkbox gets unchecked while scrolling the listview
  2. Different positioned or multiple checkboxes are checked when click on single box

In this example, we will put checkbox in both parent and child row. It means that we will create expandable listview with multiple checkboxes.

If you click on parent checkbox then all it’s child rows will be also checked. But you can also check few rows of child from the same parent as well.

After selecting required items, we will send these selected items to the next activity also.

First, see the output of the tutorial then we will develop it step by step.

Step 1. Create project in Android Studio.

Make a new project in android studio with empty activity as the default activity.

Step 2. Update colors.xml

Update your colors.xml file with following

Step 3. Adding Images

Download Images by Clicking following link

[sociallocker]Download Images[/sociallocker]

It contains three images,

  1. icon_checked,
  2. icon_unchecked and
  3. icon_dot

Now, put icon_dot into mipmap folder and copy icon_checked and icon_unchecked into drawable folder.

See below image for reference

android expandablelistview checkbox

Step 4. Adding files in drawable

Make a new layout xml named background_bordered_theme.xml and add below source code in it

Now create another layout xml file and give it a name custom_checkbox_theme.xml

Copy below code into it

Prepare third xml file named edit_text_theme.xml

Code for this file is like

Step 5. Making layout resources files

Create a new layout resource xml file in res->layout folder. It will be called child_list_layout_choose_category.xml

It’s code seems like

Make second xml file in res->layout folder and give it sweet name like group_list_layout_choose_categories.xml

Add below code in it

Prepare last xml file in res->layout and gibe its name group_list_layout_my_categories.xml

Step 6. Making Model Classes

Make a new folder called Model in app->java->your package name (It is the same path where your MainActivity is present)

See below image where you need to put required JAVA classes

android expandablelistview checkbox

Create a new JAVA class named DataItem.java in Model folder

Add Code for it like

Above class is representing the model structure for parent items or main category.

Make a second Model class named SubCategoryItem.java in Model folder

Add following code in it

Above class will word as a model for sub category or child items.

Make a new JAVA class named “ConstantManager.java” at the path shown in above image

Code for this class will look like

Prepare another JAVA class named MyCategoriesExpandableListAdapter.java

Add below source code in it

Step 7. Update Main Activity

Finally, update activity_main.xml like below

And replace code of MainActivity.java with following

We will create data for expandable listview in this activity.

Step 8. Accessing Checked Items

To get the selected parents and child items, we need to create one new activity.

We will get checked items in this activity. So make one new activity named CheckedActivity.

Add the below source code in activity_checked.xml

Write down the below code in CheckedActivity.java

I have taken one nested for loop in the above code.

Parent for loop will check which parents are selected and it will print them in the textview.

Inner for loop will get the selected child items and then print it on the another textview.

Now run your project and enjoy playing with checkboxes in expandable listview.

Support us by sharing our tutorials with other learners.

Thanks and have a nice day!

Download Source Code For Android Expandablelistview Checkbox

Click following link and download your source code

[sociallocker]Download Expandable ListView DemoChecked[/sociallocker]

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

24 Responses

  1. Riz says:

    Hi, thanks you have really very nice and helpful I used this guide and everything work fine, I want to add a clicklistener on childItem so when the child is clicked the checkbox will checked or unchecked based on previous selection. Waiting for your reply, bundle of thanks.

    • Hardik Parsania says:

      Click listener for child view is already present in adapter class.
      It is as below

      viewHolderChild.cbSubCategory.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      if (viewHolderChild.cbSubCategory.isChecked()) {
      count = 0;
      childItems.get(groupPosition).get(childPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
      notifyDataSetChanged();
      } else {
      count = 0;
      childItems.get(groupPosition).get(childPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
      notifyDataSetChanged();
      }

      for (int i = 0; i < childItems.get(groupPosition).size(); i++) { if (childItems.get(groupPosition).get(i).get(ConstantManager.Parameter.IS_CHECKED).equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) { count++; } } if (count == childItems.get(groupPosition).size()) { parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE); notifyDataSetChanged(); } else { parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE); notifyDataSetChanged(); } ConstantManager.childItems = childItems; ConstantManager.parentItems = parentItems; } });

  2. Riz says:

    Thanks Hardik, but i want if someone in childlist click on the name of item the corresponding checkbox will be checked aur unchecked, now it work only if i click on the checkbox. thanks.

    i have added clicklistner on tvSubCategoryName where i Toast the the but at same time i want that checkbox will be check aur unchecked.

    viewHolderChild.tvSubCategoryName.setOnClickListener(new View.OnClickListener()

    thanks

  3. Riz says:

    Thanks Hardik, but i want clicklistner on tvSubCategoryName that can check and uncheck the corresponding checkbox. Thanks

  4. Riz says:

    Hi thanks fo Hardik, i want clicklistioner on tvSubCategoryName that can select and unselect the checkbox.
    thanks

    • Hardik Parsania says:

      In example i have put onclick on checkbox, if you want to have onclick on textview then try something like this

      viewHolderChild.tvSubCategoryName.setOnClickListener(){
      put same code here which is present in the cbSubCategory.setonclick method
      }

      Let me know if it works or not

  5. Riz says:

    Thanks Hardik, Clicklistner work on textview but i am unable to select or deselect the checkbox. can you help me to do this. thanks again.

  6. Riz says:

    yes but not work and i am unable to understand how di you check and uncheck the box. waiting for replay, bundle of thanks

  7. Riz says:

    Hardik Parsania, can you help me to sort out the issue. Thanks

  8. MOHAMMED FADHUL says:

    Hi,

    Am not able to download the source code. Could you please share via Email

    • Hardik Parsania says:

      First like demonuts facebook page, then refresh your browser.
      If you are still not able to download, then reply me, I will send you on email.

  9. Abhi says:

    Hello Hardik , i want to get selected group and child selected list on button click

  10. sanjeev acharya says:

    hello sir ,how to get the child item data on click child check boxes without using j+1 in checkedactivity..But from childarray list..ur tutuorial is awesome sir ji

  11. sanjeev acharya says:

    hello thank u but good tutorial but i want the data of child array on click child checkbox..not using j+1..

  12. Adriano says:

    It would be amazing if you could show how to do it getting data from Firebase Realtime Database

  13. Adriano says:

    Amazing code! Based on it I’ve made up a version that fetchs subCatgoryItems with data collected from Firebase

  14. Ashish says:

    I have tried to like your page but seems page is blocked so how can I download code?

    • Hardik Parsania says:

      You can try again now. If it still do not work then remove your browser history and refresh again, it will surely work.

Leave a Reply

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