Xamarin.Android Set backgroundimage in C#

I cant find a good example how to code a backgroundimage with C# code

In my project i needed to create a folder called drawable. If this is a factor.

I dont know which one to use and how to use it.


enter image description here

Code i have now. that i want to change to image.

enter image description here

1 answer

  • answered 2018-11-13 13:56 Cheesebaron

    As you see, you have two options SetBackgroundDrawable and SetBackgroundResource.

    If you don't need to modify the drawable before assigning it, then the easiest way is to use SetBackgroundResource. Since you called your drawable noconnection then it will have its Id generated as Resource.Drawable.noconnection. So combining those two you get:


    If you need to load the drawable, you should probably use ContextCompat to load it like:

    var drawable = ContextCompat.GetDrawable(this, Resource.Drawable.noconnection);
    // do whatever with drawable
    // like tinting:
    DrawableCompat.SetTint(drawable, someTintColor);