-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Fixed Material controls themeing issue #30415
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
[Android] Fixed Material controls themeing issue #30415
Conversation
There was a problem hiding this 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 Android Material controls are themed correctly by introducing a context wrapper, updating handlers to use it, and adding repro tests.
- Added a
MauiMaterialContextThemeWrapperto wrap Android contexts with the Material theme. - Updated
MauiMaterialButtonandCheckBoxHandler.Androidto use the new theme wrapper. - Added a HostApp sample page and corresponding UI tests to validate the fix for Issue #13356.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Core/src/Platform/Android/MauiMaterialContextThemeWrapper.cs | New wrapper class to apply the correct Material theme to Android views. |
| src/Core/src/Platform/Android/MauiMaterialButton.cs | Updated button constructors to use MauiMaterialContextThemeWrapper. |
| src/Core/src/Handlers/CheckBox/CheckBoxHandler.Android.cs | Updated MaterialCheckBox instantiation to use the theme wrapper. |
| src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue13356.cs | Added UITests for Material button and checkbox dialogs on Android. |
| src/Controls/tests/TestCases.HostApp/Issues/Issue13356.cs | Added HostApp sample page and dialog implementation for Issue #13356. |
Comments suppressed due to low confidence (1)
src/Controls/tests/TestCases.HostApp/Issues/Issue13356.cs:45
- The dialog's Button is missing an AutomationId, so the test
WaitForElement("Dialog Button")may not locate it. Consider setting anAutomationIdon this Button (e.g.,AutomationId = "DialogButton") to match the test or update the test to query by text explicitly.
Content = new Button { Text = "Dialog Button", Padding = new Thickness(30) }
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue13356.cs
Outdated
Show resolved
Hide resolved
|
|
||
| namespace Microsoft.Maui.Platform; | ||
|
|
||
| internal class MauiMaterialContextThemeWrapper : ContextThemeWrapper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each call wraps the context in a new ContextThemeWrapper. This is lightweight but worth monitoring if frequently recreated during inflation.
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
* Fixed Exception * Optimized code * Updated fix * fixed checkbox * enabled android test * Fixed typo
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!
Root cause
The problem is that the
MauiMaterialButtonstyle has a parent ofWidget.MaterialComponents.Button.UnelevatedButton, which requires the application theme to be based on Theme.MaterialComponents. However, theMauiMaterialButtonconstructor is wrapping the context with just the button style, not ensuring the proper theme hierarchy.Description of change
Theme.MaterialComponent) to avoid runtime exceptions. It introduces a newMauiMaterialContextThemeWrapperto ensure the correct theme is applied to Material components and adds test cases to validate the changes.Fix for Material Component Theme Issue:
src/Core/src/Platform/Android/MauiMaterialContextThemeWrapper.cs: AddedMauiMaterialContextThemeWrapperclass to wrap the Android context with the appropriate Material theme (Resource.Style.Maui_MainTheme_Base). This ensures Material components use the correct theme.src/Core/src/Handlers/CheckBox/CheckBoxHandler.Android.cs: UpdatedCheckBoxHandlerto useMauiMaterialContextThemeWrapper.Create(Context)when creatingMaterialCheckBox, ensuring the correct theme is applied.src/Core/src/Platform/Android/MauiMaterialButton.cs: ModifiedMauiMaterialButtonto useMauiMaterialContextThemeWrapper.Create(context)for all constructors, ensuring consistent theme application for Material buttons.Reference
maui/src/Compatibility/Material/src/Android/MaterialButtonRenderer.cs
Lines 42 to 43 in aac6557
https://github.com/dotnet/maui/blob/main/src/Compatibility/Material/src/Android/MaterialContextThemeWrapper.cs
Validated the behaviour in the following platforms
Issues Fixed
Fixes #13356
Output
After.mov