setBackgroundDrawable() deprecated

Makoto picture Makoto · Nov 26, 2014 · Viewed 84.7k times · Source

So my sdk goes from 15 to 21 and when I call setBackgroundDrawable(), Android Studio tells me that it's deprecated.

I thought of going around it using:

int sdk = android.os.Build.VERSION.SDK_INT;

if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm));
} else {
    layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));
}

But then, I get an error at "setBackground()".

So, how would you deal with it?

Answer

Alex K picture Alex K · Nov 26, 2014

It's an interesting topic. The way you are doing it is correct, apparently. It is actually just a naming decision change. As this answer points out, setBackground() just calls setBackgroundDrawable():

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

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

You can see this thread for more information about all of this.