Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/Core/src/Platform/Android/CheckBoxExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<AppCompatCheckBox, ColorStateList> _defaultButtonTintCache = new();

public static void UpdateBackground(this AppCompatCheckBox platformCheckBox, ICheckBox check)
{
var paint = check.Background;
Expand All @@ -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);
}
}
}
}
Loading