setBackground vs setBackgroundDrawable (Android) setBackground vs setBackgroundDrawable (Android) android android

setBackground vs setBackgroundDrawable (Android)


It's deprecated but it still works so you could just use it. But if you want to be completly correct, just for the completeness of it... You'd do something like following:

int sdk = android.os.Build.VERSION.SDK_INT;if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {    setBackgroundDrawable();} else {    setBackground();}

For this to work you need to set buildTarget api 16 and min build to 7 or something similar.


You can use setBackgroundResource() instead which is in API level 1.


seems that currently there is no difference between the 2 functions, as shown on the source code (credit to this post) :

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

so it's just a naming decision, similar to the one with fill-parent vs match-parent .