Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

http://img193.imageshack.us/img193/2060/expandablelistviewbutto.png

like image 876
Natanael Avatar asked May 29 '12 01:05

Natanael


2 Answers

Android ExpandableListView can have any buttons in Group or child.

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

editButton.setFocusable(false);

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

like image 75
Jijo Thomas Avatar answered Nov 13 '22 19:11

Jijo Thomas


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:id="@+id/FrameLayoutGroupView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ButtonOfMyExpandableListGroupView"
        android:visibility="visible" />

</FrameLayout>

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);
        convertView.setTag(holder);
        } else {
        holder = (ViewHolder) convertView.getTag();
        }
        holder.position = ListOfItems.get(groupPosition).getPosition();
        Button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Button " + groupPosition + " is clicked !", Toast.LENGTH_SHORT).show();
               // DO STUFF
        }
    });
}

Hope this helps.

like image 2
Thordax Avatar answered Nov 13 '22 17:11

Thordax