[Android] Implemented UseMaterial3 build property and provided basic M3 styles#33074
[Android] Implemented UseMaterial3 build property and provided basic M3 styles#33074PureWeen merged 6 commits intodotnet:mainfrom
UseMaterial3 build property and provided basic M3 styles#33074Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 33074Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 33074" |
There was a problem hiding this comment.
Pull request overview
This PR introduces foundational support for Material Design 3 (M3) on Android in .NET MAUI. It adds a new MSBuild property UseMaterial3 to allow opt-in to M3, implements a runtime feature switch for detecting M3 enablement, and provides comprehensive M3 Android resources (colors and styles). The default behavior remains Material Design 2 for backward compatibility.
Key Changes:
- Adds
IsMaterial3Enabledruntime feature switch with MSBuild integration - Dynamically selects Material 2 or Material 3 themes at runtime in
MauiMaterialContextThemeWrapper - Provides complete M3 color tokens and style definitions for light/dark themes
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Core/src/RuntimeFeature.cs | Adds IsMaterial3Enabled property to expose M3 feature state at runtime |
| src/Core/src/Platform/Android/MauiMaterialContextThemeWrapper.cs | Updates constructor to conditionally use M3 or M2 base theme based on feature flag |
| src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets | Integrates UseMaterial3 MSBuild property with runtime feature switch configuration |
| src/Core/src/Platform/Android/Resources/values/styles-material3.xml | Defines M3 theme hierarchy including base theme, no action bar variant, action mode, and splash theme |
| src/Core/src/Platform/Android/Resources/values/colors-material3.xml | Provides comprehensive M3 color tokens for light/dark themes including primary, secondary, tertiary, surface, and state overlay colors |
| <resources> | ||
| <!-- Material 3 Base Theme (Automatically switches between light/dark) --> | ||
| <style name="Maui.Material3.Theme.Base" parent="Theme.Material3.DayNight"> | ||
| <!-- For .NET 9 we optout of edge to edge enforcement by default --> |
There was a problem hiding this comment.
The comment "optout" should be "opt out" (two words). This matches the correct spelling used in the original Material 2 styles file (styles.xml line 6).
| <!-- For .NET 9 we optout of edge to edge enforcement by default --> | |
| <!-- For .NET 9 we opt out of edge to edge enforcement by default --> |
|
/rebase |
bfbac3c to
b5d2aed
Compare
PureWeen
left a comment
There was a problem hiding this comment.
Should we load the material3 theme here if the property is set?
@PureWeen , Updated the app theme based on the material3 flag. |
|
…c M3 styles (#33074) <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> This pull request introduces the foundation for Material Design 3 (M3) support in .NET MAUI for Android. The changes allow developers to opt into Material Design 3 via a new MSBuild property, provide runtime detection of the Material version, and add the necessary Android resources and styles for M3. By default, apps continue to use Material Design 2 to maintain backward compatibility. **Material Design 3 opt-in and configuration:** - Added a new MSBuild property `UseMaterial3` (default: false) to allow developers to opt into Material Design 3 for Android. Internal build properties are set based on this value, and a build log message informs developers which Material version is active. (`Microsoft.Maui.Controls.Common.targets`) [[1]](diffhunk://#diff-6d91a62f494b70d0fb3dcd4fd0020ef5f83dcbbc9c8240b5cad98fc1058c6061R14-R36) [[2]](diffhunk://#diff-6d91a62f494b70d0fb3dcd4fd0020ef5f83dcbbc9c8240b5cad98fc1058c6061R57-R76) - The Material Design version is now surfaced as a runtime feature switch (`Microsoft.Maui.RuntimeFeature.IsMaterial3Enabled`), which is set during the build process if `UseMaterial3` is specified. (`Microsoft.Maui.Controls.targets`, `RuntimeFeature.cs`) [[1]](diffhunk://#diff-59cefde4ef74a9adcaff4aa8bc3271bd2d795d2b4214d7d56327b5edcca73c14R354-R357) [[2]](diffhunk://#diff-0fd447e39a83800f1f629c61f23d5a18a5c4d3d30fa3ecd81dbafbc15eb3a402R30) [[3]](diffhunk://#diff-0fd447e39a83800f1f629c61f23d5a18a5c4d3d30fa3ecd81dbafbc15eb3a402R151-R158) **Runtime and theming support:** - Introduced `MaterialDesignHelper` for runtime detection of Material Design 3 enablement, with caching for performance and a method to retrieve the current Material version. (`MaterialDesignHelper.cs`) - Updated `MauiMaterialContextThemeWrapper` to dynamically select the correct base theme (Material 2 or Material 3) at runtime using the new helper. (`MauiMaterialContextThemeWrapper.cs`) **Android resource additions for Material Design 3:** - Added comprehensive Material Design 3 color definitions for both light and dark themes, including compatibility colors for legacy components. (`colors-material3.xml`) - Added Material Design 3 style definitions, including base theme, no action bar variant, action mode, and splash theme. (`styles-material3.xml`) ### Issues Fixed <!-- Please make sure that there is a bug logged for the issue being fixed. The bug should describe the problem and how to reproduce it. --> Fixes #33073 <!-- Are you targeting main? All PRs should target the main branch unless otherwise noted. --> ### Output | Material 2 | Material 3| |--|--| | <img width="1080" height="1920" alt="M2" src="https://github.com/user-attachments/assets/877c4133-6f08-40da-90c3-d685d9e1b882" /> | <img width="1080" height="1920" alt="M3" src="https://github.com/user-attachments/assets/e2cebb4b-8438-4858-b453-414c2e03b504" /> |
Description of Change
This pull request introduces the foundation for Material Design 3 (M3) support in .NET MAUI for Android. The changes allow developers to opt into Material Design 3 via a new MSBuild property, provide runtime detection of the Material version, and add the necessary Android resources and styles for M3. By default, apps continue to use Material Design 2 to maintain backward compatibility.
Material Design 3 opt-in and configuration:
UseMaterial3(default: false) to allow developers to opt into Material Design 3 for Android. Internal build properties are set based on this value, and a build log message informs developers which Material version is active. (Microsoft.Maui.Controls.Common.targets) [1] [2]Microsoft.Maui.RuntimeFeature.IsMaterial3Enabled), which is set during the build process ifUseMaterial3is specified. (Microsoft.Maui.Controls.targets,RuntimeFeature.cs) [1] [2] [3]Runtime and theming support:
MaterialDesignHelperfor runtime detection of Material Design 3 enablement, with caching for performance and a method to retrieve the current Material version. (MaterialDesignHelper.cs)MauiMaterialContextThemeWrapperto dynamically select the correct base theme (Material 2 or Material 3) at runtime using the new helper. (MauiMaterialContextThemeWrapper.cs)Android resource additions for Material Design 3:
colors-material3.xml)styles-material3.xml)Issues Fixed
Fixes #33073
Output