Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android: getting color value from colors.xml programmatically

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)'
like image 371
Marcin Zdunek Avatar asked Jan 11 '16 14:01

Marcin Zdunek


1 Answers

Try this code:

  Color t = new Android.Graphics.Color (ContextCompat.GetColor (this, Resource.Color.row_a)); 
like image 100
FetFrumos Avatar answered Oct 01 '22 04:10

FetFrumos