I want to set background drawable of a view. There are two methods for this (as far as I see): setBackground
and setBackgroundDrawable
.
When I use setBackground
, it says it has been added in API level 16 but my project's min SDK version is 7. I assume it's not going to work on anything below 16, am I right? But when I use setBackgroundDrawable, it says it's deprecated.
What am I supposed to use?
you could use
setBackgroundResource()
instead i.e.relativeLayout.setBackgroundResource(R.drawable.back);
this works for me.
This works for me: View view is your editText, spinner...etc. And int drawable is your drawable route example (R.drawable.yourDrawable)
i know this is an old question but i have a similar situation ,and my solution was
and then you can play with the "Drawable", applying color filters, etc
Use
ViewCompat.setBackground(view, background);
Now you can use either of those options. And it is going to work in any case. Your color can be a HEX code, like this:
A color resource, like this:
Or a custom xml resource, like so:
Hope it helps!
seems that currently there is no difference between the 2 functions, as shown on the source code (credit to this post) :
so it's just a naming decision, similar to the one with fill-parent vs match-parent .