Matthew Barnden Matthew Barnden - 13 days ago 14
Android Question

Xamarin Android SetBackgroundDrawable deprecated but not SetBackground()

var textView = parentView.FindViewById<TextView>(Resource.Id.txt_chat_message);
GradientDrawable gd = new GradientDrawable();
gd.SetCornerRadius(10);
gd.SetColor(Color.Yellow);
textView.SetBackgroundDrawable(gd);


As in the example above
SetBackgroundDrawable
allows me to control the color and radius programmatically. I have looked at
SetBackgroundResouce
but I cannot find a clear example as it just seems to take an ID to a resource that I could not change progamatically.

Could someone help me providing an alternative that gives me the flexibility to do the exact same as
SetBackgroundDrawable
above please?

Answer

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;