How do I set a drawable as the background for a list view in a class?
if (array1.size() < 8)
{
lv1.setBackgroundDrawable(R.drawable.bgimghs2b);
}
(: or use
lv1.setBackground(R.drawable.bgimghs2b);
Use this: android.view.View.setBackgroundResource(int resID)
lv1.setBackgroundResource(R.drawable.bgimghs2b);
update: That method is deprecated, instead you can use:
Just API 16 or above.
setBackground(Drawable background)
if you have a previous version that API 16 use:
setBackgroundResource(int resid)
You should use:
Drawable background = this.getResources().getDrawable(R.drawable.yourBackgroundDrawableID);
lv.setBackgroundDrawable(background);
That's because you're not giving it a Drawable
, but an ID of a drawable. Try:
lv1.setBackgroundDrawable(getResources().getDrawable(R.drawable.bgimghs2b))
If you're in an activity. If not, then you need to get a Context
and call getResources()
on that.