I am using an ExpandableList via ExpandableListViewActivity.
Now, I would like to change the text colour of a certain TextView inside each ChildView. I don't want them all to be the same, rather something like "ok" in green and "error" in red etc.
I did stumble across getExpandableListAdapter().getChildView(...), but I'm unsure what the last parameters (View convertView, ViewGroup parent) are supposed to be. Also I don't quite know if I need to cast the return value (View) into something?
TextView tv = (TextView)this.getExpandableListAdapter().getChildView(0, 0, false, ???, ???).findViewById(R.id.xlist_child_tv);
Kind regards, jellyfish
Solution summary
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(...parameters...){
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
final TextView tv = (TextView) itemRenderer.findViewById(R.id.kd_child_value);
if (tv.getText().toString().contentEquals(getString(R.string.true)))
{
tv.setTextColor(getResources().getColor(R.color.myred));
}
else if (tv.getText().toString().contentEquals(getString(R.string.false)))
{
tv.setTextColor(getResources().getColor(R.color.mygreen));
}
else
{
tv.setTextColor(getResources().getColor(R.color.white));
}
return itemRenderer;
}
};
color resource:
<resources>
<color name="white">#ffffffff</color>
<color name="myred">#FFFF3523</color>
<color name="mygreen">#FFA2CD5A</color>
</resources>
You should override the getChildView
method of your ExpandableListAdapter
implementation, and inside of it set the text color based on any flag accessible to this adapter.
If the change is explicit, don't forget to call notifyDatasetChanged()
on the adapter after changing the flag!
To override the getChildView
method of a SimpleExpandableListAdapter
, you need to extend it (here anonymously):
final SimpleExpandableListAdapter sa = new SimpleExpandableListAdapter(/*your list of parameters*/)
{
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
final View itemRenderer = super.getChildView(groupPosition,
childPosition, isLastChild, convertView, parent);
final TextView tv = (TextView)itemRenderer.findViewById(R.id.text);
if (/*check whether the data at groupPosition:childPosition is correct*/)
tv.setTextColor(getResources().getColor((R.color.green));
else
tv.setTextColor(getResources().getColor((R.color.red));
return itemRenderer;
}
};
Update
The problem in coloring lies in your resources file, you need to set the alpha value too for a text color, so your red should be #FFFF3523
, and your green: #FFA2CD5A
:
<resources>
<color name="white">#ffffffff</color>
<color name="myred">#FFFF3523</color>
<color name="mygreen">#FFA2CD5A</color>
</resources>