joaoasilva joaoasilva - 1 year ago 318
Android Question

Get color from Xamarin.Forms.Color on android drawable XML

Is something like this possible on a drawable xml file?

<stroke android:width="1dp" android:color="Xamarin.Forms.Color.Red.ToAndroid" />

I want to be able to have a list of colors on my shared project and be able to use them on the XML files on android?


Answer Source

Via code, you can use the Xamarin.Forms.Platform.Android.ColorExtensions to convert from a Xamarin.Forms color to an Android.Graphics.Color.

Within your Xamarin.Android project (custom Forms renderers, etc...):

Android.Graphics.Color aColor = Xamarin.Forms.Color.Blue.ToAndroid()

Xamarin.Forms.Color is based on W3C standards so you can use these as your Android color xml resource file to provide the same colors within your custom AXML-based drawables/layout/etc...

<?xml version="1.0" encoding="utf-8"?>
 <color name="white">#FFFFFF</color>
 <color name="yellow">#FFFF00</color>
 <color name="fuchsia">#FF00FF</color>
 <color name="red">#FF0000</color>
 <color name="silver">#C0C0C0</color>
 <color name="gray">#808080</color>
 <color name="olive">#808000</color>
 <color name="purple">#800080</color>
 <color name="maroon">#800000</color>
 <color name="aqua">#00FFFF</color>
 <color name="lime">#00FF00</color>
 <color name="teal">#008080</color>
 <color name="green">#008000</color>
 <color name="blue">#0000FF</color>
 <color name="navy">#000080</color>
 <color name="black">#000000</color>

Note: It is not possible to use code based / dynamic variables within AXML files

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download