How to convert Android.Resource.Color to Android.Graphics.Color

13,653

Solution 1

Resources.GetColor has been deprecated. Make sure you use ContextCompat.GetColor instead.

So

Resources.GetColor(Resource.Color.HoloOrangeDark);

Will become

int colorInt = ContextCompat.GetColor(this.Context, Resource.Color.HoloOrangeDark))

ContextCompat returns a int so to get a Color out of it just create a new color and pass in the int.

Color color = new Color(colorInt);

Solution 2

You can try this:

Resources.GetColor(Resource.Color.HoloOrangeDark);

UPDATE:
Resources.GetColor has been deprecated. You can use from ContextCompat like below:

ContextCompat.GetColor(mContext, Resource.Color.HoloOrangeDark);
Share:
13,653

Related videos on Youtube

Code Pope
Author by

Code Pope

Software developer for more than a decade. "From my point of view, code is the light that illuminates the darkness, even if it does not dissolve it, and a spark of divine light is within each of us."

Updated on July 08, 2022

Comments

  • Code Pope
    Code Pope almost 2 years

    I have a value of Android.Resource.Color like Android.Resource.Color.HoloOrangeDark.
    I want to covert it to a Android.Graphics.Color.
    How can I do that?

    • Code Pope
      Code Pope almost 10 years
      @pskink Android.Resource.Color is an enum value in Xamarin.Android representing constant integers for the Android.R.Color class in Android. So Android.Resource.Color is equal to the Anroid.R.Color in Android. The values are constant integers.
    • pskink
      pskink almost 10 years
      so Resources.getColor() should probably work
  • Mohammad Afrashteh
    Mohammad Afrashteh about 6 years
    this.Context not exist!
  • CDrosos
    CDrosos about 6 years
    so just use this on activities. this.Context is usually used of fragments