12
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?

Wosi
  • 41,986
  • 17
  • 75
  • 82
Matthew Barnden
  • 516
  • 1
  • 6
  • 15

3 Answers3

20

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;
Jason
  • 86,222
  • 15
  • 131
  • 146
  • But I cannot change the radius using `Background` alone (unless I am being dumb, which is entirely possible : ) – Matthew Barnden Nov 10 '15 at 18:04
  • Background is still a Drawable, so you should be able to use the same approach as before. See my edit – Jason Nov 10 '15 at 18:24
  • I'm not sure if this is a Xamarin thing but your edit blows up, It's a little late in the day now so I will try and debug tomorrow and find the reason why, thanks for help. much appreciated. – Matthew Barnden Nov 10 '15 at 18:47
  • This is the error I am receiving in the output when trying to do as you have suggested `java.lang.NoSuchMethodError: no method with name='setBackground' signature='(Landroid/graphics/drawable/Drawable;)V' in class Landroid/view/View;` – Matthew Barnden Nov 11 '15 at 11:27
  • My emulator was running on 3.1 needs to be 4.12 or higher for this to work, many thanks. For reference see this report https://bugzilla.xamarin.com/show_bug.cgi?id=26624 – Matthew Barnden Nov 11 '15 at 12:41
  • for anyone else I used a check for version like so `BuildVersionCodes sdk = Build.VERSION.SdkInt; if (sdk < BuildVersionCodes.JellyBean) { TextView.SetBackgroundDrawable(gd); } else { TextView.Background = gd; }` – Matthew Barnden Nov 11 '15 at 13:16
4

Instead of SetBackgroundDrawable(back); use Background = back;

You have to do like this:

back = context.GetDrawable(Resource.Drawable.cover);
// SetBackgroundDrawable(back);
Background = back;`
kayess
  • 3,384
  • 9
  • 28
  • 45
3

Edit

Based on running it on device it seems Background property is not completely implemented at the moment (11.11.2015). My trial and error approach points out that setting Background through the property is throwing exception that it didn't find the setBackground method with appropriate arguments. So the issue is not with the new way of getting the drawables but when you try to set them. Maybe I'm misusing this so I'm open for corrections.

//Works
yesButton.SetBackgroundDrawable(ContextCompat.GetDrawable(context, Resource.Drawable.selector_green_button));

//Works
yesButton.SetBackgroundDrawable(ResourcesCompat.GetDrawable(Resources, Resource.Drawable.selector_green_button, Resources.NewTheme()));

//Doesn't Work     
yesButton.Background = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.selector_green_button, Resources.NewTheme());

//Doesn't Work
yesButton.Background = ContextCompat.GetDrawable(context, Resource.Drawable.selector_green_button);

//Doesn't Work
yesButton.Background = Resources.GetDrawable(Resource.Drawable.selector_green_button);

Original Answer

You can use the Background property as @Jason Suggested already.

In order to use that you'll need to get the Drawable now the funny part:

GetDrawable method is deprecated (since API 22 i think) so you should be using :

someControl.Background = ContextCompat.GetDrawable(context, Resource.Drawable.your_drawable);

instead of Resources.GetDrawable(Resource.Drawable.your_drawable);

Found this link for reference: More info on getting drawables in Android native

Community
  • 1
  • 1
kirotab
  • 1,296
  • 1
  • 11
  • 18
  • Thanks but I am not sure this plays well with Xamarin, I will also look further into this tomorrow, thanks also for your suggestion and link. – Matthew Barnden Nov 10 '15 at 18:54
  • I just ran it on the emulator before I post but not tested on device, will do tomorrow at work (it requires `using Android.Support.V4.Content`) but VS will let you know :) – kirotab Nov 10 '15 at 19:06