xamarin set view background color from a hex value

15,975

Solution 1

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'

Solution 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'
Share:
15,975

Related videos on Youtube

CBaker
Author by

CBaker

Updated on September 14, 2022

Comments

  • CBaker
    CBaker over 1 year

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

    For example

     view.BackgroundColor = Color.FromHex("#00162E");
    
  • CBaker
    CBaker about 9 years
    I get an error saying Color does not contain a definition for FromHex.
  • LawfulEvil
    LawfulEvil about 9 years
    You are working cross platform or are you just targeting ios or android or something?
  • CBaker
    CBaker about 9 years
    just targeting android. Im not using Xamarin Forms
  • ClintL
    ClintL about 9 years
    If you are not using forms you may want to take a look at stackoverflow.com/questions/5248583/…