2

I have an ExpendableListView, associated with a BaseExpandableListAdapter. In this list, when the user clicks on a child, it triggers some methods ans sets a checkBox to checked (same thing if the user clicks again, but the chackBox goes unchecked).

To be able to use the onChildClick method properly, I have set the checkBox to android:enabled="false", as suggested by numerous posts.

The thing is, the checkBox automatically becomes grey, checked or not. It doesn't affect the behaviour of the activity, but it is not very visual (unlike when the check is light green).

How can I keep exactly the same behaviour, but with a colored checkBox?

Or in other terms, is it possible (and is it simple) to use android:enabled="false" and to keep the color of the view?

Here is the xml code for the checkBox :

<CheckBox android:id="@+id/config_can_spn_checkbox"
android:layout_marginRight="30dip"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:enabled="false"
android:focusable="false" />

The BaseExpandableListAdapter Class :

public class ConfigCanAdapter extends BaseExpandableListAdapter {

private Context context;
private List<Pgn> pgnList;
private LayoutInflater inflater;

public ConfigCanAdapter(Context context, List<Pgn> pgnList) {
    this.context = context;
    this.pgnList = pgnList;
    inflater = LayoutInflater.from(context);
}

@Override
public boolean areAllItemsEnabled() {
    return true;
}

public Spn getChild(int gPosition, int cPosition) {
    return pgnList.get(gPosition).getSpnList().get(cPosition);
}

public long getChildId(int gPosition, int cPosition) {
    return cPosition;
}

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

    final Spn spn = (Spn) getChild(groupPosition, childPosition);

    ChildViewHolder childViewHolder;

    if (convertView == null) {
        childViewHolder = new ChildViewHolder();

        convertView = inflater.inflate(R.layout.group_child, null);

        childViewHolder.textViewChildSpnName = (TextView) convertView.findViewById(R.id.config_can_spn_name_text);
        childViewHolder.textViewChildSpnUnit = (TextView) convertView.findViewById(R.id.config_can_spn_unit_text);
        childViewHolder.checkBoxChildSpn = (CheckBox) convertView.findViewById(R.id.config_can_spn_checkbox); 

        convertView.setTag(childViewHolder);
    } else {
        childViewHolder = (ChildViewHolder) convertView.getTag();
    }

    childViewHolder.textViewChildSpnName.setText(spn.getName());

    childViewHolder.textViewChildSpnUnit.setText("(" + spn.getUnit() + ")");        

    return convertView;
}

public int getChildrenCount(int gPosition) {
    return pgnList.get(gPosition).getSpnList().size();
}

public Object getGroup(int gPosition) {
    return pgnList.get(gPosition);
}


public int getGroupCount() {
    return pgnList.size();
}

public long getGroupId(int gPosition) {
    return gPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    GroupViewHolder gholder;
    Pgn pgn = (Pgn) getGroup(groupPosition);

    if (convertView == null) {
        gholder = new GroupViewHolder();

        convertView = inflater.inflate(R.layout.group_row, null);

        gholder.textViewGroup = (TextView) convertView.findViewById(R.id.TVGroup);

        convertView.setTag(gholder);
    } else {
        gholder = (GroupViewHolder) convertView.getTag();
    }

    gholder.textViewGroup.setText(pgn.getId());

    return convertView;
}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}

class GroupViewHolder {
    public TextView textViewGroup;
}

class ChildViewHolder {
    public TextView textViewChildSpnName;
    public TextView textViewChildSpnUnit;
    public CheckBox checkBoxChildSpn;
}

}

And finally the onChildClickListener :

expandableList.setOnChildClickListener(new OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        // TODO Auto-generated method stub
        CheckBox cb = (CheckBox)v.findViewById(R.id.config_can_spn_checkbox);
        cb.toggle();
        Toast.makeText(getBaseContext(), "Group = " + String.valueOf(groupPosition) + " child = " + String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
        return false;
    }
});
WhiskThimble
  • 547
  • 3
  • 10
  • 23

3 Answers3

1

You can just set android:clickable="false" and android:focusable="false" that will take away all the clicks and it wont take focus but keep the enabled look like you want

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • As far as I understand it, `android:focusable="false"` removes the focus from the checkBox, allowing the `onChildClickListener` to be used. `android:clickable="false"` removes the trigger of the `onClickListeners` but it doesn't prevent the user from clicking on the checkBox. Therefore, the state changes, but the code of the `onChildClickListener` isn't trigger. That is what I basically want to avoid – WhiskThimble Jun 18 '13 at 15:37
0

You can override the selector for your button to something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_enabled="false" android:drawable="@drawable/image_enabled" />
   <item android:drawable="@drawable/image_enabled" />
</selector>

Not sure if just this code will suit you, but it is an idea :) check here and here for a tutorial.

Neron T
  • 369
  • 2
  • 8
  • In fact, it doesn't work either, because the drawables will still appear greyed out... – WhiskThimble Jun 18 '13 at 15:41
  • No, the drawables will appear as you want them... remember you are overriding them. Just think as you were doing a custom button from scratch. – Neron T Jun 18 '13 at 15:43
  • check this post too: http://stackoverflow.com/questions/5790454/disable-button-with-custom-background-android – Neron T Jun 18 '13 at 15:46
0

Instead of disabling the checkbox you could use a click listener to intercept clicks and change the value back. That sounds like a bit of a hack; you could alternatively subclass the view and override the undesirable behaviour that way.

Phil Haigh
  • 4,522
  • 1
  • 25
  • 29