meepz
meepz

Reputation: 363

Android set background resource and color

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

Upvotes: 4

Views: 19118

Answers (4)

BHA Bilel
BHA Bilel

Reputation: 371

Just add a ColorFilter after setting your background ressource to the view like this

view.setBackgroundResource(R.drawable.yourRessource);
view.getBackground().setColorFilter(
                Color.yourColor,
                PorterDuff.Mode.DST_OVER
);

Learn more about Manipulating images and Drawables with Android’s ColorFilter

Upvotes: 0

lenooh
lenooh

Reputation: 10682

If you want to use setBackgroundResource and setBackgroundColor you can do this:

...
int colorPos = groupPosition % colors.length;
convertView.setBackgroundResource(R.drawable.row_forground);
GradientDrawable drawable = (GradientDrawable) convertView.getBackground();
drawable.setColor(color2[colorPos]);
...

Upvotes: 5

nandeesh
nandeesh

Reputation: 24820

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

Upvotes: 10

Warlock
Warlock

Reputation: 2711

There is for sure code which can take background drawable and use paint some color on it. But I can't rember now :-)
But there is other ways how can you achieve your goal. Look at this site:
http://developer.android.com/guide/topics/resources/drawable-resource.html
Look at 9patch drawables which you can prepare, which will be only small images which will shrink/expand as needed. You prepare one with and other color inside.
The second way is to use ShapeDrawable. In XML you create rectangle and some solid color inside it.
In both cases you just swap background drawables as needed.
I don't understand what you want exactly achieve, but I hope that this can help.

Upvotes: 1

Related Questions