4

I just upgraded my Android project's build target to API 17, and I'm now getting warnings about setBackgroundDrawable being deprecated. The answer appears to be to use setBackground, but that's not available in older versions.

Is there any actual advantage to using the new method, or did Google just want to change the name? I don't see any point in complicating my code with platform version checks or reflection if the two work the same.

olane
  • 117
  • 1
  • 6

1 Answers1

8

Is there any actual advantage to using the new method, or did Google just want to change the name?

They seemed to only want to change the name, look at the source code:

public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

@Deprecated
public void setBackgroundDrawable(Drawable background) { ... }

All of the work is still done in setBackgroundDrawable(). For now, you can ignore the deprecation warnings but understand that in some future API setBackgroundDrawable() will be removed.


In case you are curious, setBackgroundResource(int resid) simply creates a drawable from the resource ID and calls setBackground() (which again calls setBackgroundDrawable())...

Sam
  • 86,580
  • 20
  • 181
  • 179
  • 2
    For anyone looking at this now 4.4 STILL has the same code that just forwards to setBackgroundDrawable() http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/view/View.java#View.setBackground%28android.graphics.drawable.Drawable%29 – sgarman Jan 15 '14 at 01:11
  • @sgarman Thanks for the link! I also checked the version 5.1 there, and STILL the same code. – Jenix Mar 02 '16 at 15:54