Take the 2-minute tour ×

How to set a drawable as background for a listview in a class?

if (array1.size() < 8)

is improper.


share|improve this question
What do you mean by "improper"? –  CommonsWare Jun 4 '11 at 15:52
Probably that it doesn't compile. –  dmon Jun 4 '11 at 15:59
add comment

5 Answers

up vote 56 down vote accepted

That's because you're not giving it a Drawable, but an ID of a drawable. Try:


If you're in an activity. If not, then you need to get a Context and call getResources() on that.

share|improve this answer
Thanks! i've used once, but couldn't find :S –  erdomester Jun 4 '11 at 17:24
+1 for useful answer –  Lucifer Nov 18 '12 at 15:40
deprecated method. –  meh Nov 28 '12 at 8:20
To elaborate: listView.setBackgroundDrawable(Drawable) was deprecated in 4.1 (API Level 16). –  dmon Nov 28 '12 at 14:40
add comment

use this: android.view.View.setBackgroundResource(int resID)


share|improve this answer
add comment

you should use : Drawable background = this.getResources().getDrawable(R.drawable.yourBackgroundDrawableID); lv.setBackgroundDrawable(background);

share|improve this answer
add comment

update: That method is deprecated, instead you can use:

  1. Just API 16 or above.

    setBackground(Drawable background)

  2. if you have a previous version that API 16 use:

    setBackgroundResource(int resid)

share|improve this answer
add comment

(: or use

share|improve this answer
add comment

Your Answer


