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.
Difference between setBackgroundDrawable() and setBackground()
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()
)...
setBackgroundDrawable() deprecated
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.
Xamarin Android SetBackgroundDrawable deprecated but not SetBackground()
Use the Background property. Generally whenever Android has a getX/setX method with no arguments, Xamarin converts it to a C# style property named X.
var textView = parentView.FindViewById<TextView>(Resource.Id.txt_chat_message);
GradientDrawable gd = new GradientDrawable();
gd.SetCornerRadius(10);
gd.SetColor(Color.Yellow);
textView.Background = gd;
setBackgroundDrawable in android
Use register.setBackgroundResource(R.drawable.btn_save);
.
public void setBackgroundResource (int resid)
is available from API Level 1.
View.setBackground throws NoSuchMethodError
This method was introduced in API level 16, you are most probably running on an earlier one:
http://developer.android.com/reference/android/view/View.html#setBackground(android.graphics.drawable.Drawable)
Use setBackgroundDrawable()
Related Topics
How to Add a Border to the Top and Bottom of an Android View
Android Youtube App Play Video Intent
How to Add Manifest Permission to an Application
Android View.Getdrawingcache Returns Null, Only Null
Error Inflating Class Fragment
Outofmemoryerror: Bitmap Size Exceeds Vm Budget :- Android
How to Disable Landscape Mode in Android
How to Dynamically Update a Listview on Android
How to Resume Existing Fragment from Backstack
How to Save Recyclerview'S Scroll Position Using Recyclerview.State
Dialog With Transparent Background in Android
What Is Activity.Finish() Method Doing Exactly
Android - Sms Broadcast Receiver
How to Solve Java.Lang.Outofmemoryerror Trouble in Android
How to Set Navigation Drawer to Be Opened from Right to Left