Maria Georgali Maria Georgali - 6 months ago 35
Android Question

How I can have different background color in different expandable ListView Group in Android

I have a Tab Layout, each layout has a expandable ListView, each ListView has its BaseExpandableListAdapter. I want for each group a different background. How I am supposed to change my Adapter to achieve this?

public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
String listTitle = (String) getGroup(listPosition);
if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.off_group, null);
}
TextView listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}


I tried the suggested, and I declare the int color array with hex colors globally! like

private int[] colors = {Color.parseColor("#b8dbd3"), Color.parseColor("#ffe975"), Color.parseColor("#dbdcff"), Color.parseColor("#f5f5f5"), Color.parseColor("#ccd6dd")};

public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
String listTitle = (String) getGroup(listPosition);
if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.off_group, null);
}
Random ranndom = new Random();
int ranndomColor = ranndom.nextInt(5);
LinearLayout GroupLayout = (LinearLayout) convertView.findViewById(R.id.ParentGroup);
GroupLayout.setBackgroundResource(colors[ranndomColor]);

TextView listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}


but I get an error on resource

FATAL EXCEPTION:
main android.content.res.Resources$NotFoundException: Resource ID #0xffffff00

Answer

try this:

 private int[] colors = {Color.RED, Color.YELLOW, Color.BLUE, Color.CYAN, Color.GREEN};

 Random ranndom = new Random();
 int ranndomColor = ranndom.nextInt(5);
 whatever.setBackground(colors[ranndomColor]);