diff --git a/src/Core/src/Platform/Android/CheckBoxExtensions.cs b/src/Core/src/Platform/Android/CheckBoxExtensions.cs index a7e9c8717586..55fad63e537c 100644 --- a/src/Core/src/Platform/Android/CheckBoxExtensions.cs +++ b/src/Core/src/Platform/Android/CheckBoxExtensions.cs @@ -1,4 +1,5 @@ -using Android.Content.Res; +using System.Runtime.CompilerServices; +using Android.Content.Res; using Android.Graphics; using AndroidX.AppCompat.Widget; using AndroidX.Core.Widget; @@ -9,6 +10,12 @@ namespace Microsoft.Maui.Platform { public static class CheckBoxExtensions { + // Store the original theme button tint per checkbox instance to support: + // - Per-Activity theming (different Activities can have different themes) + // - Theme switching at runtime (dark mode, Material2/3 toggle) + // - Thread safety (no shared mutable state) + static readonly ConditionalWeakTable _defaultButtonTintCache = new(); + public static void UpdateBackground(this AppCompatCheckBox platformCheckBox, ICheckBox check) { var paint = check.Background; @@ -34,21 +41,39 @@ public static void UpdateForeground(this AppCompatCheckBox platformCheckBox, ICh internal static ColorStateList GetColorStateList(this AppCompatCheckBox platformCheckBox, ICheckBox check) { - AColor tintColor; + // Cache the original theme button tint for this checkbox instance before we modify it. + // This must happen before SetButtonTintList is called, as that will overwrite ButtonTintList. + if (!_defaultButtonTintCache.TryGetValue(platformCheckBox, out var defaultButtonTintList)) + { + var currentTint = platformCheckBox.ButtonTintList; + if (currentTint is not null) + { + _defaultButtonTintCache.Add(platformCheckBox, currentTint); + defaultButtonTintList = currentTint; + } + } // For the moment, we're only supporting solid color Paint for the Android Checkbox if (check.Foreground is SolidPaint solid) { var color = solid.Color; - tintColor = color.ToPlatform(); + AColor tintColor = color.ToPlatform(); + return ColorStateListExtensions.CreateCheckBox(tintColor); } - else + + if (RuntimeFeature.IsMaterial3Enabled) { - Graphics.Color accent = platformCheckBox.Context?.GetAccentColor() ?? Graphics.Color.FromArgb("#ff33b5e5"); - tintColor = accent.ToPlatform(); + // Material 3: Use the original theme's buttonTint + if (defaultButtonTintList is not null) + { + return defaultButtonTintList; + } } - return ColorStateListExtensions.CreateCheckBox(tintColor); + // Material 2: Use accent color + Graphics.Color accent = platformCheckBox.Context?.GetAccentColor() ?? Graphics.Color.FromArgb("#ff33b5e5"); + AColor tintColor2 = accent.ToPlatform(); + return ColorStateListExtensions.CreateCheckBox(tintColor2); } } -} \ No newline at end of file +}