Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setBackgroundResource() discards my XML layout attributes

I have a view which is used as an item in a ListView. In my custom adapter, I change the background of the view using View.setBackgroundResource() depending on the item's position in the list. (I have separate assets for the first and last items in the list.)

This sets the correct background image as expected, but it has the nasty side-effect that all the padding I'd set in the XML definition of the view is completely ignored.

(If I set the background drawable in the XML, and don't try to vary it at runtime in the adapter, the padding all works fine.)

How can I alter the background image, and retain the padding? Is this a bug?

EDIT it seems someone else has found the same problem here: Does changing the background also change the padding of a LinearLayout?

like image 213
Graham Borland Avatar asked May 04 '11 22:05

Graham Borland


3 Answers

I ran into this issue as well. Presumably you're using a LayerList resource drawable? That's what I was using. Unfortunately, I found no "real" way of fixing it, it seems like a bug in the code, but I didn't chase it down. However, I was lucky in the sense that I was setting the "buggy" background after my view had already been rendered properly, so it was just a matter of saving then restoring the padding values after the background is set, e.g:

  if(condition) {
    int bottom = theView.getPaddingBottom();
    int top = theView.getPaddingTop();
    int right = theView.getPaddingRight();
    int left = theView.getPaddingLeft();
    theView.setBackgroundResource(R.drawable.entry_bg_with_image);
    theView.setPadding(left, top, right, bottom);
  }

EDIT: As an alternative, you don't have to use the previous values of padding, you can also use a dimension value:

  int pad = resources.getDimensionPixelSize(R.dimen.linear_layout_padding);
  theView.setBackgroundResource(R.drawable.entry_bg_with_image);
  theView.setPadding(pad, pad, pad, pad);
like image 147
dmon Avatar answered Nov 10 '22 04:11

dmon


Adding onto what dmon has suggested, here is a function you can just throw in your util class so you don't have to jump through hoops every time you update a resource. This is really just his code wrapped in a function.

public static void updateBackgroundResourceWithRetainedPadding(View view, int resourceID)
{
    int bottom = view.getPaddingBottom();
    int top = view.getPaddingTop();
    int right = view.getPaddingRight();
    int left = view.getPaddingLeft();
    view.setBackgroundResource(resourceID);
    view.setPadding(left, top, right, bottom);
}
like image 31
Brandon Romano Avatar answered Nov 10 '22 04:11

Brandon Romano


This is fixed in Lollipop, so

public static void setBackgroundResource(@NonNull View view, @DrawableRes int resId) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        int paddingTop = view.getPaddingTop();
        int paddingLeft = view.getPaddingLeft();
        int paddingRight = view.getPaddingRight();
        int paddingBottom = view.getPaddingBottom();
        view.setBackgroundResource(resId);
        view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    } else {
        view.setBackgroundResource(resId);
    }
}
like image 7
e.shishkin Avatar answered Nov 10 '22 03:11

e.shishkin