From 08e282350cb3f1c2345125682f14934111af2f9c Mon Sep 17 00:00:00 2001 From: Michael Rumpler Date: Tue, 2 Aug 2016 22:54:40 +0200 Subject: [Android] Color.Accent is hardcoded (#270) --- Xamarin.Forms.Platform.Android/Forms.cs | 47 ++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'Xamarin.Forms.Platform.Android') diff --git a/Xamarin.Forms.Platform.Android/Forms.cs b/Xamarin.Forms.Platform.Android/Forms.cs index 62e07f22..a2a063c9 100644 --- a/Xamarin.Forms.Platform.Android/Forms.cs +++ b/Xamarin.Forms.Platform.Android/Forms.cs @@ -96,19 +96,7 @@ namespace Xamarin.Forms ResourceManager.Init(resourceAssembly); - // Detect if legacy device and use appropriate accent color - // Hardcoded because could not get color from the theme drawable - var sdkVersion = (int)Build.VERSION.SdkInt; - if (sdkVersion <= 10) - { - // legacy theme button pressed color - Color.Accent = Color.FromHex("#fffeaa0c"); - } - else - { - // Holo dark light blue - Color.Accent = Color.FromHex("#ff33b5e5"); - } + Color.Accent = GetAccentColor(); if (!IsInitialized) Log.Listeners.Add(new DelegateLogListener((c, m) => Trace.WriteLine(m, c))); @@ -148,6 +136,39 @@ namespace Xamarin.Forms IsInitialized = true; } + static Color GetAccentColor() + { + Color rc; + using (var value = new TypedValue()) + { + if (Context.Theme.ResolveAttribute(global::Android.Resource.Attribute.ColorAccent, value, true)) // Android 5.0+ + { + rc = Color.FromUint((uint)value.Data); + } + else if(Context.Theme.ResolveAttribute(Context.Resources.GetIdentifier("colorAccent", "attr", Context.PackageName), value, true)) // < Android 5.0 + { + rc = Color.FromUint((uint)value.Data); + } + else // fallback to old code if nothing works (don't know if that ever happens) + { + // Detect if legacy device and use appropriate accent color + // Hardcoded because could not get color from the theme drawable + var sdkVersion = (int)Build.VERSION.SdkInt; + if (sdkVersion <= 10) + { + // legacy theme button pressed color + rc = Color.FromHex("#fffeaa0c"); + } + else + { + // Holo dark light blue + rc = Color.FromHex("#ff33b5e5"); + } + } + } + return rc; + } + class AndroidDeviceInfo : DeviceInfo { readonly IDeviceInfoProvider _formsActivity; -- cgit v1.2.3