1

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.

Jerielle
  • 5,994
  • 27
  • 75
  • 146

4 Answers4

5

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

Community
  • 1
  • 1
Harshad
  • 1,256
  • 1
  • 9
  • 25
  • Thanks for the information. I tried the first solution and I imported the missing classes and now the warning is gone. :) – Jerielle Feb 04 '16 at 06:49
1

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.

1

May be this image will solve your problem

this is Documentation given by Android itself

Rjz Satvara
  • 3,175
  • 2
  • 18
  • 41
1
getDrawable(int) method was deprecated in API level 22.

Use getDrawable(int, Theme)

sree
  • 683
  • 1
  • 11
  • 25