8

How do i programatically set the background color of a view using xamarin from the hex value

For example

 view.BackgroundColor = Color.FromHex("#00162E");
Jason
  • 86,222
  • 15
  • 131
  • 146
CBaker
  • 830
  • 2
  • 10
  • 25
  • possible duplicate of [How to get a Color from hexadecimal Color String](http://stackoverflow.com/questions/5248583/how-to-get-a-color-from-hexadecimal-color-string) – Jason Apr 16 '15 at 20:23

4 Answers4

12

I believe you're looking for the ParseColor method that takes in a string and returns the integer color.

view.BackgroundColor = Android.Graphics.Color.ParseColor("#FF6A00");

Supported formats are:

  • #RRGGBB
  • #AARRGGBB
  • 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
Thomas
  • 6,291
  • 6
  • 40
  • 69
ripple182
  • 801
  • 1
  • 8
  • 13
2

From their example.. I think you just need to drop the #.

view.BackgroundColor = Color.FromHex("FF6A00")

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/colors/

Depending on your target, you may want to try the parseColor method on the color class instead.

How to get a Color from hexadecimal Color String

public static int parseColor (String colorString)

It claims to take hex values :

... formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
Community
  • 1
  • 1
LawfulEvil
  • 2,267
  • 24
  • 46
0

To change the color programatically of some text in the .xaml file you can do this, in for instance a switch loop:

FindViewById<TextView>(Resource.Id.->insert the id/name from the .xaml file <-).SetBackgroundColor(Color.Red);
break;

I hope it helps

ToFo
  • 1,643
  • 1
  • 19
  • 34
0

Worked for me this:

 BackgroundColor = Color.FromHex("#fcf2e4");

My Xamarin.Forms Version: 5.0.0.2012.

I found Code Here

LY

LY

Gonçalo Garrido
  • 126
  • 2
  • 11