Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getDrawable(int) from the type Resources is deprecated in Eclipse Android

I am following some tutorials in android about how to create an app that can toggle the silent mode. Everything is fine until I got this errors:

enter image description here

The method getDrawable(int) from the type Resources is deprecated

Can you help me with this? I am new in Android. By the way I am using API 23 for this.

like image 593
Jerielle Avatar asked Jul 26 '26 09:07

Jerielle


2 Answers

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

like image 56
Harshad Avatar answered Jul 28 '26 05:07

Harshad


This is just a warning telling you that there is no support for this method by google and you shouldn't use it. You can use getDrawable(R.drawable.phone_silent,null); This null is for theme. If you have specific theme then you can provide theme here else null will suffice. Please accept if it helps.

like image 34
Damanpreet Singh Avatar answered Jul 28 '26 03:07

Damanpreet Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!