8

How can I get value of color programmatically from colors.xml file into C# code?

Here is my colors.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <item name="row_a" type="color">#FFCCFFCC</item>
  <item name="row_b" type="color">#FFFFFFCC</item>
  <item name="all_text" type="color">#FF000000</item>
  <item name="row_red" type="color">#FFFF4444</item>
  <item name="row_orange" type="color">#FFE69900</item>
  <item name="row_green" type="color">#FF739900</item>
  <item name="wheat" type="color">#FFF5DEB3</item>

  <integer-array name="androidcolors">
    <item>@color/row_a</item>
    <item>@color/row_b</item>
    <item>@color/all_text</item>
    <item>@color/row_red</item>
    <item>@color/row_orange</item>
    <item>@color/row_green</item>
    <item>@color/wheat</item>
  </integer-array>

</resources>

I tried:

Color t = (Color)Resource.Colors.wheat;

but of course I cannot convert int value to Color this way.

EDIT:

As suggested I tried

Color t = Resources.GetColor(Resource.Color.row_a);

But it gives me an error:

Error   CS0120  An object reference is required for the non-static field, 
method, or property 'Resources.GetColor(int)'
0

3 Answers 3

12

Try this code:

  Color t = new Android.Graphics.Color (ContextCompat.GetColor (this, Resource.Color.row_a)); 
3
  • ContextCompat does not even contain GetColor method! any ideas? Commented Apr 16, 2017 at 16:44
  • Forget it. Updating the package Xamarin.Android.Support.V4 from NuGet fixed the issue. I was trying an old sample which contained an old version of the package so it was not showing up. Commented Apr 16, 2017 at 16:55
  • Android.Support.V4.Content.ContextCompat.GetColor(this, Resource.Color.my_color)
    – Pierre
    Commented Jun 20, 2017 at 18:08
8

Problem was that I tried to access Resources from ListView Adapter. Solution is to use:

parent.Resources.GetColor(Resource.Color.row_a)

where parent is passed into public override View GetView(int position, View convertView, ViewGroup parent) method.

3
  • 3
    This method is depricated now, now you can do it this way new Android.Graphics.Color(ContextCompat.GetColor(this, Resource.Color.color_name)) Commented Apr 6, 2017 at 21:20
  • 2
    @CodeIt Out of curiosity, do you know why the Resources.GetColor method was deprecated? This new ContextCompat way of doing things seems extremely verbose Commented Jul 19, 2018 at 16:43
  • EVERYTHING in Android is verbose. Look how much trouble it is to get a simple color when you want it. If we did our jobs the way they do theirs, we would be fired...
    – Alan
    Commented May 13, 2022 at 5:42
0
public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstancesState)
    {
        // check your Resources/Resource.designer.cs file
        int nResId = Resource.Color.row_a;
        var colorValue = ApplicationContext.GetColor(nResId);
    }
}

Android.Content.Context.GetColor

Android Resources

Not the answer you're looking for? Browse other questions tagged or ask your own question.