Home » Android » android – How to put a button inside group item in an ExpandableListView?

android – How to put a button inside group item in an ExpandableListView?

Posted by: admin June 15, 2020 Leave a comment


I’m developing an application for Android.

How could I put a button in a group of a ExpandableListView?

By clicking the button a dialog will be displayed instead of open or close the group. Click outside the button, the group should behave normally opening and closing.

The image below shows where I would like to insert the button.


How to&Answers:

Android ExpandableListView can have any buttons in Group or child.

Make sure that the button is not focusable like below in adapter.


this will help to click on Group and Button inside group.parent seperately


You need to inflate the groupView with a custom XML file containing a button, like this one ( e.g inflate_xml_groupview.xml ) :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:visibility="visible" />


Then you have to create a custom ExpandableListAdapter that extends BaseExpandableListAdapter and get the Button on the getGroupView() method, like this :

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {

        convertView = inflater.inflate(R.layout.inflate_xml_groupview, null);
        holder = new ViewHolder();
        holder.Button = (Button) convertView.findViewById(R.id.myButton);
        } else {
        holder = (ViewHolder) convertView.getTag();
        holder.position = ListOfItems.get(groupPosition).getPosition();
        Button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Button " + groupPosition + " is clicked !", Toast.LENGTH_SHORT).show();
               // DO STUFF

Hope this helps.


To offer an XML based solution, you simply need to add the following line to the control.





I created my own ExpandableListView. I used layouts in XML and classes to build the component.

And surprisingly it was very easy to do.

It was much easier to understand than the standard ExpandableListView because I created a class and a layout for each element of the list (for the list itself, for the group and for the items). There was no need messing with lists of lists of maps, which in my opinion decreases the expressivity and the readability of the code.

Additionally, the list becomes extremely flexible and customizable. I can easily add and remove groups and items at runtime. Now I can freely modify the appearance and internal components of the list.

The ExpandableListView I created can do the same as the standard and more. Just can not tell if the performance was impaired, but did not notice any visible problem.