ContextCompat.getColor if API < 23 (#5450)
authorkingces95 <kingces95@users.noreply.github.com>
Thu, 7 Mar 2019 17:18:57 +0000 (07:18 -1000)
committerSamantha Houts <samhouts@users.noreply.github.com>
Thu, 7 Mar 2019 17:18:57 +0000 (09:18 -0800)
Update Xamarin.Forms.Platform.Android/Forms.cs

Does this do all the suggestions?

Co-Authored-By: kingces95 <kingces95@users.noreply.github.com>
ContextCompat.getColor if API < 23

Xamarin.Forms.Platform.Android/Forms.cs
Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs

index b52f4c1..ee4b2be 100644 (file)
@@ -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)
index 294a747..04e7cae 100644 (file)
@@ -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)