Android getResources().getDrawable() deprecated API 22
You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:
A) drawables with theme attributes
ContextCompat.getDrawable(getActivity(), R.drawable.name);
You'll obtain a styled Drawable as your Activity theme instructs.
This is probably what you need.
B) drawables without theme attributes
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable()
is not deprecated!
EXTRA) drawables with theme attributes from another theme
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
getDrawable(int) from the type Resources is deprecated in Eclipse Android
getResources().getDrawable()
is now deprecated.
You should use the following code from the support library instead:
ContextCompat.getDrawable(context, R.drawable.***)
Using this method is equivalent to calling:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
As of API 21, you should use the getDrawable(int, Theme)
method instead of getDrawable(int
), as it allows you to fetch a drawable object associated with a particular resource ID for the given screen density/theme. Calling the deprecated getDrawable(int)
method is equivalent to calling getDrawable(int, null)
.
for more detail visit Here.
Android getResources().getDrawable() deprecated API 22
getDrawable(int) is deprecated warning in this line: Drawable res = getContext().getResources().getDrawable(item.img_resId);
Now you should use
Drawable drawable = ContextCompat.getDrawable(this,R.drawable.my_drawable);
Where this
is your passed context
if you are using a widget, just this
if you are calling from an Activity, or getActivity()
if you are calling from Fragment
In your case, when you are initializing your Widget also save your Context
instance:
Context context;
public CustomAdapter(Context context, ArrayList<ItemDataObject> list_item_details) {
super(context, 0, list_item_details);
this.context = context;
}
Now you are able to call:
Drawable drawable = ContextCompat.getDrawable(context,R.drawable.my_drawable);
Where is the deprecated API at my class code ? I can't figure out
In Android API 22 getResources().getDrawable()
was deprecated. Now you should use this method getDrawable (int id, Resources.Theme theme)
And getColor (int id)
was deprecated in API 23. Now use getColor(int, android.content.res.Resources.Theme)
instead.
Check this out for more info:
https://developer.android.com/reference/android/content/res/Resources
Android getDrawable() Deprecated / How To use Android getDrawable()
Since API 22 you should call ContextCompat.getDrawable(context, R.drawable.***)
instead of getResources().getDrawable(R.drawable.***)
.
You can see the documentation of ContextCompat from the support library for more details.
Issues with getDrawable compatibilty
You can try using ContextCompat.getDrawable(getActivity(), R.drawable.name);
. This will return a styled Drawable as instructed by your Activity's theme. Your code would be more or less as shown below:
buttonItem.setImageDrawable(ContextCompat.getDrawable(getActivity(), painting.getId()));
NOTE: I'm assuming that painting.getId()
returns the resource id for the image you want to set as your button's drawable. Also, if you are doing this inside an Activity, you should use the keyword this
instead of getActvity()
.
Android: Alternative for context.getDrawable()
The previously accepted method has been deprecated, according to the SDK 22 documentation:
Prior to android.os.Build.VERSION_CODES#JELLY_BEAN, this function would not correctly retrieve the final configuration density when the resource ID passed here is an alias to another Drawable resource. This means that if the density configuration of the alias resource is different than the actual resource, the density of the returned Drawable would be incorrect, resulting in bad scaling.
As pointed out in this answer better solution would be to use ContextCompat:ContextCompat.getDrawable(context, R.drawable.***)
getDrawable(int) is deprecated and may produce a NullPointerException
getDrawable()
is deprecated in API level 22.
Use ContextCompat
for that visit this :
https://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getDrawable(android.content.Context,%20int)
Use this way.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
As mention Here. Android getResources().getDrawable() deprecated API 22
Related Topics
Creating a System Overlay Window (Always on Top)
How to Use Getdownloadurl in Recent Versions
How to Get Extra Data from Intent on Android
Libraries Do Not Get Added to Apk Anymore After Upgrade to Adt 22
Retrieve a Fragment from a Viewpager
Install Application Programmatically on Android
Difference Between Gravity and Layout_Gravity in Android
How to Correctly Save Instance State of Fragments in Back Stack
Sending and Parsing Json Objects in Android
How to Determine When Fragment Becomes Visible in Viewpager
Get Filename and Path from Uri from Mediastore
Android Imageview Zoom-In and Zoom-Out
Adb Server Version Doesn't Match This Client