Skip to content

[Android] Switch have different track color in light mode when application started in dark mode - fix #29804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Jun 3, 2025

…ation started in dark mode

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

For fellow developers 🙂

If this PR doesn’t get merged, you can still apply the fix by adding the following code to your custom handlers.

Microsoft.Maui.Handlers.SwitchHandler.Mapper.AppendToMapping<Switch, ISwitchHandler>(nameof(ISwitch.TrackColor),(handler, view) =>
{
	if (view.IsToggled)
	{
		return;
	}

	if (handler.PlatformView is AndroidX.AppCompat.Widget.SwitchCompat aSwitch)
	{
		//https://android.googlesource.com/platform/frameworks/support/+/9d5f84f/v7/appcompat/res/color/abc_tint_switch_track.xml
		var isDarkTheme = Application.Current?.UserAppTheme != AppTheme.Unspecified
			? Application.Current?.UserAppTheme == AppTheme.Dark
			: Application.Current.PlatformAppTheme == AppTheme.Dark;

		var trackMaterial = isDarkTheme ? Resource.Color.foreground_material_dark : Resource.Color.foreground_material_light;
		var trackColor = new Android.Graphics.Color(AndroidX.Core.Content.ContextCompat.GetColor(handler.PlatformView.Context, trackMaterial));

		var alphaChannel = view.IsEnabled ? 0.3f : 0.1f;
		var trackColorWithAlpha = new Android.Graphics.Color(trackColor.R, trackColor.G, trackColor.B, (int)(trackColor.A * alphaChannel));

		aSwitch.TrackDrawable?.SetColorFilter(trackColorWithAlpha, Android.Graphics.PorterDuff.Mode.SrcIn);
	}
});

Issues Fixed

Fixes #29687

@Copilot Copilot AI review requested due to automatic review settings June 3, 2025 14:29
@kubaflo kubaflo requested a review from a team as a code owner June 3, 2025 14:29
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue with the Switch track color on Android when the application starts in dark mode by updating the track color mapping logic for Switch controls.

  • Removed the clearing of color filters in UpdateTrackColor.
  • Introduced a new mapping method for Switch track color in the Controls project.
  • Updated the app host builder to include the updated Switch mapping.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Core/src/Platform/Android/SwitchExtensions.cs Removed call to ClearColorFilter in UpdateTrackColor
src/Controls/src/Core/Switch/Switch.Mapper.cs Added a new mapping (MapTrackColor) to set the track color conditionally
src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs Added call to Switch.RemapForControls in the app builder

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jun 3, 2025
Copy link
Contributor

Hey there @@kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@Pastajello
Copy link

Pastajello commented Jul 18, 2025

Sadly, when you change the Theme of the application in runtime, the suggested fix doesn't work until the switch is triggered again. Also the "on" color for track is same as "off" color for track with the fix :(

That aside, why would the fix not be merged if the issue is clear?

@kubaflo

Edit
Ah now I see that track color is being set in the code, but now that the track color is set it's not being changed between on and off color of track

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] Switch have different track color in light mode when application started in dark mode
2 participants