Robert Kuykendall
Robert Kuykendall

Reputation: 300

Android: Checkbox in ExpandableListView not triggering onChildClick even with focusable=false

Goal: I am trying to create a UI with 2 checkboxes per line, each line seperated into sections. I've included a screenshot of the UI below.

Current approach: I am using an ExpandableListView and handeling the data with an onChildClick.

Problem: When you click a checkbox, it does not trigger onChildClick or anything else. Clicking anywhere outside of the checkbox will trigger this event.

Research: There are lots of threads that suggest setting android:focusable="false", but that doesn't change anything for me. I have focusable set to false for every element in my UI.

Reproduction: I have the exact same problem running the code from this article without modification, which includes android:focusable="false". I based a lot of my code on that example, and If I can get it working using that codebase, I'm sure I can get it working in mine.

Current UI

Upvotes: 2

Views: 2495

Answers (3)

bergjs
bergjs

Reputation: 133

As far as I figured it out, the checkbox does consume the event so the OnChildClickListener doesn't trigger. In the tag list item I added these lines to the checkbox:

<CheckBox
    ...
    android:focusable="false"
    android:clickable="false"
    android:focusableInTouchMode="false"/>

Then I could simply use my OnChildClickListener as if I wouldn't have used a checkbox.

Upvotes: 3

Barak
Barak

Reputation: 16393

You are missing a line in your XML I bet.

You need to have these two lines in your xml (the second one being the most important):

android:focusable="false"
android:focusableInTouchMode="false"

That will allow each checkbox to be clickable separately as well as the row itself.

Then you set onCheckChangedListener listeners in your adapter getView() to deal with the checkboxes (where it will have access to the position, view and parent info). Your onChildClick can then deal with the rwo clicks.

Upvotes: 4

ixx
ixx

Reputation: 32273

I think it's because the checkbox consumes the event and doesn't forward it to the list item.

If you want to get the click on the boxes you can add OnClickListener to the boxes in the getView() of the list adapter.

I understand you right, the box is at least getting checked? If you don't need custom behaviour you just can read if the box is checked or not when you "submit" the list (or whatever is your next step of checking boxes).

After reading comments, here is your code example with some additions:

@Override
public View getView(final int pos, final View convView, ViewGroup parent) {

    //initialize convView, define other local variables, etc. 

    CheckBox cb = (CheckBox) convView.findViewById(R.id.mycheckbox);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
            Log.d("ElistCBox2", "listitem position: " + pos); 
        } 
    });

    return convView;
}

Note that you have to use final modifier for the variables which you use in the anonymous class, otherwise you get a compiler error.

Upvotes: 0

Related Questions