Skip to content

[Android] SearchBar does not update colors on theme change - fix #30601

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 2 commits into
base: main
Choose a base branch
from

Conversation

kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Jul 13, 2025

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!

Issues Fixed

Fixes #30600
Fixes #25153

Before After
Screen.Recording.2025-07-13.at.19.44.33.mov
Screen.Recording.2025-07-13.at.19.36.48.mov

@Copilot Copilot AI review requested due to automatic review settings July 13, 2025 18:08
@kubaflo kubaflo requested a review from a team as a code owner July 13, 2025 18:08
@kubaflo kubaflo self-assigned this Jul 13, 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.

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jul 13, 2025
@kubaflo kubaflo added platform/android area-controls-searchbar SearchBar control community ✨ Community Contribution and removed community ✨ Community Contribution labels Jul 13, 2025
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 ensures that the Android SearchBar’s placeholder, text, and cancel‐button colors correctly update when the app’s theme changes.

  • Update SearchViewExtensions to pull hint/text colors from the current Android theme.
  • Hook into Application.RequestedThemeChanged in SearchBar to reapply color properties.
  • Extend the Android handler mapping to call the new text‐color update logic.

Reviewed Changes

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

File Description
src/Core/src/Platform/Android/SearchViewExtensions.cs Read hint/text colors from theme and apply on placeholder, text, and cancel button
src/Core/src/Handlers/SearchBar/SearchBarHandler.Android.cs Invoke UpdateTextColor on the platform view as well as the query editor
src/Controls/src/Core/SearchBar/SearchBar.cs Subscribe/unsubscribe to theme‐change events and trigger property updates
src/Controls/src/Core/Application/Application.cs Move UserAppTheme property‐changed notification before raising the RequestedThemeChanged event
Comments suppressed due to low confidence (1)

src/Controls/src/Core/SearchBar/SearchBar.cs:134

  • There are no automated UI tests covering the dynamic theme‐change behavior for Android SearchBar. Please add a HostApp UI page and corresponding NUnit tests in TestCases.Shared.Tests to validate that placeholder, text, and cancel‐button colors update correctly.
		private void OnRequestedThemeChanged(object sender, AppThemeChangedEventArgs e)

Comment on lines +45 to 46
editText.Resources?.GetColor(typedValue.ResourceId, theme) is Color textColorHint)
{
Copy link
Preview

Copilot AI Jul 13, 2025

Choose a reason for hiding this comment

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

The pattern matching GetColor(...) is Color textColorHint will never match because GetColor returns an int, not an Android.Graphics.Color. You should capture the returned int and construct a Color (e.g., new Color(resources.GetColor(...))) before applying it.

Suggested change
editText.Resources?.GetColor(typedValue.ResourceId, theme) is Color textColorHint)
{
editText.Resources?.GetColor(typedValue.ResourceId, theme) is int colorInt)
{
var textColorHint = new Color(colorInt);

Copilot uses AI. Check for mistakes.

Comment on lines +59 to +60
editText.Resources?.GetColor(typedValue.ResourceId, theme) is Color color)
{
Copy link
Preview

Copilot AI Jul 13, 2025

Choose a reason for hiding this comment

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

Same issue here: GetColor returns an int, so the is Color color check will fail. Use the returned int to create an Android.Graphics.Color instance before calling SetTextColor.

Suggested change
editText.Resources?.GetColor(typedValue.ResourceId, theme) is Color color)
{
editText.Resources?.GetColor(typedValue.ResourceId, theme) is int colorInt)
{
var color = Color.ValueOf(colorInt);

Copilot uses AI. Check for mistakes.

Comment on lines +147 to +149
theme.ResolveAttribute(Android.Resource.Attribute.TextColorPrimary, typedValue, true) &&
image.Resources?.GetColor(typedValue.ResourceId, theme) is Color imageColor)
{
Copy link
Preview

Copilot AI Jul 13, 2025

Choose a reason for hiding this comment

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

The expression GetColor(...) is Color imageColor will never succeed since GetColor returns an int. Construct an Android.Graphics.Color from the int result to apply the tint correctly.

Suggested change
theme.ResolveAttribute(Android.Resource.Attribute.TextColorPrimary, typedValue, true) &&
image.Resources?.GetColor(typedValue.ResourceId, theme) is Color imageColor)
{
theme.ResolveAttribute(Android.Resource.Attribute.TextColorPrimary, typedValue, true))
{
var colorInt = image.Resources?.GetColor(typedValue.ResourceId, theme) ?? 0;
var imageColor = Color.ValueOf(colorInt);

Copilot uses AI. Check for mistakes.

@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

if (Application.Current == null)
return;

if (args.NewHandler == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

You're only unsubscribing when NewHandler is null, but you should unsubscribe whenever there's an OldHandler.

{
App.WaitForElement("changeThemeButton");
App.Tap("changeThemeButton");
VerifyScreenshot();
Copy link
Contributor

Choose a reason for hiding this comment

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

Pending snapshots, running a build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-searchbar SearchBar control community ✨ Community Contribution platform/android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] SearchBar does not update colors on theme change Theme issue on Search bar control in .NET MAUI
2 participants