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?
I also had this problem, but I made a workaround using a ImageView.
Try using a RelativeLayout and add a ImageView inside it (width and height: fill_parent, scaleType: center).
Also make sure the imageview is the first element inside the RelativeLayout, so it will act as background.
Using Android studio 1.5.1 i got the following warnings:
and the complaints about deprecation
Using this format, i got rid of both:
Use setBackgroundResource(R.drawable.xml/png)
You can use
setBackgroundResource()
instead which is in API level 1.It's deprecated but it still works so you could just use it. But if you want to be completly correct, just for the completeness of it... You'd do something like following:
For this to work you need to set buildTarget api 16 and min build to 7 or something similar.
You can also do this:
EDIT: Just as pointed out by @BlazejCzapp it is preferable to avoid using reflection if you can manage to solve the problem without it. I had a use case where I was unable to solve without reflection but that is not case above. For more information please take a look at http://docs.oracle.com/javase/tutorial/reflect/index.html