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;
}
});