From 92ffeb324f3f2ba68e93c69875d60bc788198fd8 Mon Sep 17 00:00:00 2001 From: kingces95 Date: Thu, 7 Mar 2019 07:18:57 -1000 Subject: [PATCH] ContextCompat.getColor if API < 23 (#5450) Update Xamarin.Forms.Platform.Android/Forms.cs Does this do all the suggestions? Co-Authored-By: kingces95 ContextCompat.getColor if API < 23 --- Xamarin.Forms.Platform.Android/Forms.cs | 11 +++++++++++ Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Xamarin.Forms.Platform.Android/Forms.cs b/Xamarin.Forms.Platform.Android/Forms.cs index b52f4c1..ee4b2be 100644 --- a/Xamarin.Forms.Platform.Android/Forms.cs +++ b/Xamarin.Forms.Platform.Android/Forms.cs @@ -30,6 +30,7 @@ namespace Xamarin.Forms const int TabletCrossover = 600; static bool? s_isLollipopOrNewer; + static bool? s_isMarshmallowOrNewer; [Obsolete("Context is obsolete as of version 2.5. Please use a local context instead.")] [EditorBrowsable(EditorBrowsableState.Never)] @@ -55,6 +56,16 @@ namespace Xamarin.Forms } } + internal static bool IsMarshmallowOrNewer + { + get + { + if (!s_isMarshmallowOrNewer.HasValue) + s_isMarshmallowOrNewer = (int)Build.VERSION.SdkInt >= 23; + return s_isMarshmallowOrNewer.Value; + } + } + public static Color GetColorButtonNormal(Context context) { if (!_ColorButtonNormalSet) diff --git a/Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs index 294a747..04e7cae 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs @@ -1,6 +1,9 @@ using System; using System.ComponentModel; using Android.Content; +using Android.Support.V4.Content; +using AColor = Android.Graphics.Color; +using AColorRes = Android.Resource.Color; using Android.Views; namespace Xamarin.Forms.Platform.Android @@ -117,7 +120,9 @@ namespace Xamarin.Forms.Platform.Android bool isDefaultBkgndColor = bkgndColor.IsDefault; if (page.Parent is BaseShellItem && isDefaultBkgndColor) { - var color = Context.Resources.GetColor(global::Android.Resource.Color.BackgroundLight, Context.Theme); + var color = Forms.IsMarshmallowOrNewer ? + Context.Resources.GetColor(AColorRes.BackgroundLight, Context.Theme) : + new AColor(ContextCompat.GetColor(Context, global::Android.Resource.Color.BackgroundLight)); SetBackgroundColor(color); } else if (!isDefaultBkgndColor || setBkndColorEvenWhenItsDefault) -- 2.7.4