Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between setBackgroundDrawable() and setBackground()

Tags:

java

android

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.

like image 766
olane Avatar asked Feb 19 '23 06:02

olane


1 Answers

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())...

like image 81
Sam Avatar answered Apr 07 '23 11:04

Sam