expandablelistview – Expandable list view in Android doesn't have reaction to click, it doesn't expand-ThrowExceptions

Exception or error:

I have an expandable listview. I have put some dummy data and evrything seems ok but when I run the application all groups are in collapse mode and they don’t take effect when I click on each of them. This is the screenshot:

enter image description here

XML of Group is:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bg_slider"
    android:orientation="horizontal" >


    <TextView
        android:id="@+id/tvRecipeName"
        style="@style/normal_text.bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="5dp"
        android:focusable="false"
        android:lines="1"
        android:maxLines="1"
        android:text="@string/dd_title" />


    <ImageButton
        android:id="@+id/ibDeleteRecipe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:background="@color/transparent"
        android:contentDescription="@string/cd"
        android:focusable="false"
        android:src="@android:drawable/ic_menu_delete" />

    <View
        android:layout_width="1dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/tvRecipeName"
        android:focusable="false" />

</RelativeLayout>

XML of child is:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:gravity="left|center_vertical" >

    <View
        android:layout_width="1dp"
        android:layout_height="35dp"
        android:layout_toLeftOf="@+id/tvIngredient"
        android:focusable="false" />

    <TextView
        android:id="@+id/tvIngredient"
        style="@style/normal_text_black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="5dp"
        android:lines="1"
        android:maxLines="1"
        android:text="@string/dd_title"
        android:focusable="false" />

    <ImageButton
        android:id="@+id/ibDeleteIngredient"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:background="@color/transparent"
        android:contentDescription="@string/cd"
        android:focusable="false"
        android:src="@android:drawable/btn_dialog" />

</RelativeLayout>

and in main.xml, definition of expandable list is:

<ExpandableListView
            android:id="@+id/elv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

I have an adapter that I have written this code for that:

public class ExpAdapter extends BaseExpandableListAdapter {

    private final String TAG = "ExpAdapter";
    private Context context;
    static final String arrGroupelements[] = {"India", "Australia", "England", "South Africa"};
    static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
                                                 {"Ponting", "Adam Gilchrist", "Michael Clarke"},
                                                 {"Andrew Strauss", "kevin Peterson", "Nasser Hussain"},
                                                 {"Graeme Smith", "AB de villiers", "Jacques Kallis"} };

    public ExpAdapter(Context context) {
        this.context = context;

        Log.i(TAG, "Adapter created.");
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.elv_child, null);
        }

        TextView tvItem = (TextView) convertView.findViewById(R.id.tvIngredient);
        ImageButton ibDelete = (ImageButton) convertView.findViewById(R.id.ibDeleteIngredient);
        ibDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "******");
            }
        });
        tvItem.setText(arrChildelements[groupPosition][childPosition]);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return arrChildelements[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return arrGroupelements.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.elv_group, null);
        }

        TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
        ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
        ibDeleteRcipe.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "%%%%%%");
            }
        });
        tvItem.setText(arrGroupelements[groupPosition]);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

and finally, in code of fragment activity i have:

public class ShoppingList extends FragmentActivity {    
        ExpAdapter adapter = new ExpAdapter(this);
        //Linking expnadable list view
        expListView = (ExpandableListView) findViewById(R.id.elv);
        expListView.setAdapter(adapter);
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Log.i(TAG, "Group " + groupPosition + " expanded.");
            }
        });
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Log.i(TAG, "Group " + groupPosition + " collapsed.");
            }
        });
        expListView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Log.i(TAG, "item " + childPosition + " of group " + groupPosition + " clicked.");
                return false;
            }
        });

}

When I run the application and click on parents, nothing happens. I tried to add following lines of code at the end of shopping class:

int count = adapter.getGroupCount();
for (int position = 1; position <= count; position++)
    expListView.expandGroup(position - 1);

Now when I run the application result is like this screenshot:

enter image description here

If I click on delete buttons (parent or child), I can see they take effect (as I check logcat), however when I click on child or parent nothing happens. So, I have no idea why callbacks doesn’t work. Based on my research I found that I need to set Focussable of image buttons to false. As you can see in XML files I did it but still when I click on parent and child rows I can’t see any response.

How to solve:

Finally I found the solution 🙂

I have no idea is it bug or disease or …

As mentioned above based on my research I found that we need to set focus-ability of image view/box to “false”. I did it in XML file and it didn’t work. I set focus-ability to “true” in XML and in code set it to false. like this:

@Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.elv_group, null);
        }

        TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
        ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
        ibDeleteRcipe.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
                ...
            }
        });
        ibDeleteRcipe.setFocusable(false);

        tvItem.setText(arrGroupElements[groupPosition]);

        return convertView;
    }

Therefore based of my achievement! it should be set to “false” in code not in XML! Now the question is What is difference? -I have no idea.

###

It is necessary to define android:focusable="false" in the layout xml for focusable views within the expandable list view item but not in the “ExpandableListView” attribute.

Eg: if a custom parent list item includes a checkbox (it will automatically get the focus) it should be like below.

<CheckBox
   android:id="@+id/cbkSelectDownload"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:focusable="false"/>

Leave a Reply

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