Reputation: 164
how can I set an xml background file that placed in drawable for a view without using @SuppressLint("NewApi")
?
for example I created a drawable xml file for my textview
when I call TV.setBackground(getResources().getDrawable(R.drawable.tv_pic_back));
eclipse automatically add @SuppressLint("NewApi")
at the first of my function.
how can I use that without @SuppressLint("NewApi")
?
Upvotes: 1
Views: 5394
Reputation: 13341
I have a class where I put a lot of code to handle the different APIs, so that you use one line of code for one API, and another line of code for another API.
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
}
else {
view.setBackgroundDrawable(drawable);
}
}
This will still give you a warning because setBackgroundDrawable
is deprecated, but if you instead would use setBackground(drawable)
for all versions then your application would crash on API levels lower than Jelly Bean (API 16).
However, in your case all you need to do is actually setBackgroundResource(R.drawable.tv_pic_back);
because you don't need to get the drawable from the resource id yourself, Android will do that for you if you give it your resource id when you call the right method.
The Android developer reference will tell you which methods are deprecated and which methods are implemented in which API version.
Upvotes: 4