0

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>
jellyfish
  • 7,868
  • 11
  • 37
  • 49

1 Answers1

2

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>
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • I'm sorry, I don't understand... what flags? And why getChildView? Is it always called when I use "setListAdapter"? Atm I'm only using the default SimpleExpandableListAdapter, so I'm not too familiar with customizing the Adapter. – jellyfish May 10 '11 at 10:10
  • You can override the `getChildView` method of that `SimpleExpandableListAdapter` too. Retrieve the automatic item renderer using `super.getChildView`, and on that call `findViewById(R.id.your_colored_textview)` to access the text view to which you'd like to assign a new color. The `getChildView` method is called when a child item is about to show up, or is changed an needs to be refreshed. – rekaszeru May 10 '11 at 10:15
  • please see my update for a sample implementation. (also: you should call `setListAdapter` only once!) – rekaszeru May 10 '11 at 10:22
  • Wow, thank you very much! However, I don't get the colors working... the text just becomes black (so you can't even read it). I've posted my code above. Color resources are ok, I've checked in the XML files. – jellyfish May 10 '11 at 11:39
  • thanks for the addition, please see my update for the correct text color definition. – rekaszeru May 10 '11 at 11:52
  • thank you again! unfortunately, this doesn't help either, the color just stays black. :( – jellyfish May 10 '11 at 12:07
  • I've updated my answer as well, it was my mistake. Instead of `setColor(your color resource id)` you should retrieve the color value, and that should be set: `setTextColor(getResources().getColor(your color resource id))` – rekaszeru May 10 '11 at 12:21
  • woohoo! Thank you ever, ever so much for your time! :) – jellyfish May 10 '11 at 12:33