Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set background resource and color

Tags:

android

Is it possible to set the backgroundResource and backgroundColor of a ListView/ExpandableListView in a custom adapter.

I have a transparent png that will serve as the border for each row in an expandable listview. This image is just an inner shadow and a border on the bottom.

The goal is to do something like this:

png + color

When I set the backgroundResource and then backgroundColor, only one of the two shows up. I can't get the resource to overlay the color. Does anyone know if this is possible?

Here is my code to get a better idea:

private int[] colors2= new int[] { Color.parseColor("#e2e8e9"), Color.parseColor("#f1f2f2") };
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ViewHolder holder;
    ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.expandlist_group_item, null);
        holder.title = (TextView) convertView.findViewById(R.id.tvGroup);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    int colorPos = groupPosition % colors.length; 
    convertView.setBackgroundResource(R.drawable.row_forground);
    convertView.setBackgroundColor(color2[colorPos]);
    holder.title.setText(group.getName());
    return convertView;
}
like image 306
meepz Avatar asked Dec 06 '22 12:12

meepz


1 Answers

setBackgroundResource and setBackgroundColor both use the same api setBackgroundDrawable internally to do their tasks. So one overwrites the other. So you wont be able to achieve your goal using this api.

You will have to use setBackgroundResource with a custom drawable

like image 153
nandeesh Avatar answered Dec 23 '22 21:12

nandeesh