xref: /aosp_15_r20/external/accompanist/docs/themeadapter-material3.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1# Material 3 Theme Adapter
2
3[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-themeadapter-material3)](https://search.maven.org/search?q=g:com.google.accompanist)
4
5!!! warning
6    **This library is deprecated, and the API is no longer maintained. We recommend generating a theme with [Material Theme Builder](https://m3.material.io/theme-builder)** The original documentation is below.
7
8## Migration
9Recommendation: Use the [Material Theme Builder](https://m3.material.io/theme-builder) tool, or an alternative design tool, to generate a matching XML and Compose theme implementation for your app. See [Migrating XML themes to Compose](https://developer.android.com/jetpack/compose/designsystems/views-to-compose) to learn more.
10
11You can checkout [Material Design 3 in Compose](https://developer.android.com/jetpack/compose/designsystems/material3#material-theming) to learn more about creating and adding theme to your app using Material Theme Builder.
12
13## Original Documenation
14A library that enables the reuse of [MDC-Android][mdc] Material 3 XML themes, for theming in [Jetpack Compose][compose].
15
16![Material 3 Theme Adapter header](themeadapter/material3-header.png)
17
18The basis of Material Design 3 theming in [Jetpack Compose][compose] is the [`MaterialTheme`][materialtheme] composable, where you provide [`ColorScheme`][colorscheme], [`Typography`][typography] and [`Shapes`][shapes] instances containing your styling parameters:
19
20``` kotlin
21MaterialTheme(
22    colorScheme = colorScheme,
23    typography = typography,
24    shapes = shapes
25) {
26    // M3 Surface, Scaffold, etc.
27}
28```
29
30[Material Components for Android][mdc] themes allow for similar theming for views via XML theme attributes, like so:
31
32``` xml
33<style name="Theme.MyApp" parent="Theme.Material3.DayNight">
34    <!-- Material 3 color attributes -->
35    <item name="colorPrimary">@color/purple_500</item>
36    <item name="colorSecondary">@color/purple_700</item>
37    <item name="colorTertiary">@color/green_200</item>
38
39    <!-- Material 3 type attributes-->
40    <item name="textAppearanceBodyLarge">@style/TextAppearance.MyApp.BodyLarge</item>
41    <item name="textAppearanceBodyMedium">@style/TextAppearance.MyApp.BodyMedium</item>
42
43    <!-- Material 3 shape attributes-->
44    <item name="shapeAppearanceCornerSmall">@style/ShapeAppearance.MyApp.CornerSmall</item>
45</style>
46```
47
48This library attempts to bridge the gap between [Material Components for Android][mdc] M3 XML themes, and themes in [Jetpack Compose][compose], allowing your composable [`MaterialTheme`][materialtheme] to be based on the `Activity`'s XML theme:
49
50
51``` kotlin
52Mdc3Theme {
53    // MaterialTheme.colorScheme, MaterialTheme.typography, MaterialTheme.shapes
54    // will now contain copies of the Context's theme
55}
56```
57
58This is especially handy when you're migrating an existing app, a `Fragment` (or other UI container) at a time.
59
60### Customizing the M3 theme
61
62The [`Mdc3Theme()`][mdc3theme] function will automatically read the host `Context`'s MDC theme and pass them to [`MaterialTheme`][materialtheme] on your behalf, but if you want to customize the generated values, you can do so via the [`createMdc3Theme()`][createmdc3theme] function:
63
64``` kotlin
65val context = LocalContext.current
66var (colorScheme, typography, shapes) = createMdc3Theme(
67    context = context
68)
69
70// Modify colorScheme, typography or shapes as required.
71// Then pass them through to MaterialTheme...
72
73MaterialTheme(
74    colorScheme = colorScheme,
75    typography = typography,
76    shapes = shapes
77) {
78    // Rest of M3 layout
79}
80```
81
82### Limitations
83
84There are some known limitations with the implementation at the moment:
85
86* This relies on your `Activity`/`Context` theme extending one of the `Theme.Material3` themes.
87* Text colors are not read from the text appearances by default. You can enable it via the `setTextColors` function parameter.
88* Variable fonts are not supported in Compose yet, meaning that the value of `android:fontVariationSettings` are currently ignored.
89* MDC `ShapeAppearances` allow setting of different corner families (cut, rounded) on each corner, whereas Compose's [Shapes][shapes] allows only a single corner family for the entire shape. Therefore only the `app:cornerFamily` attribute is read, others (`app:cornerFamilyTopLeft`, etc) are ignored.
90* You can modify the resulting `MaterialTheme` in Compose as required, but this _only_ works in Compose. Any changes you make will not be reflected in the `Activity` theme.
91
92---
93
94## Usage
95
96[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-themeadapter-material3)](https://search.maven.org/search?q=g:com.google.accompanist)
97
98``` groovy
99repositories {
100    mavenCentral()
101}
102
103dependencies {
104    implementation "com.google.accompanist:accompanist-themeadapter-material3:<version>"
105}
106```
107
108### Library Snapshots
109
110Snapshots of the current development version of this library are available, which track the latest commit. See [here](../using-snapshot-version) for more information on how to use them.
111
112---
113
114## Contributions
115
116Please contribute! We will gladly review any pull requests.
117Make sure to read the [Contributing](../contributing) page first though.
118
119## License
120
121```
122Copyright 2022 The Android Open Source Project
123
124Licensed under the Apache License, Version 2.0 (the "License");
125you may not use this file except in compliance with the License.
126You may obtain a copy of the License at
127
128    https://www.apache.org/licenses/LICENSE-2.0
129
130Unless required by applicable law or agreed to in writing, software
131distributed under the License is distributed on an "AS IS" BASIS,
132WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133See the License for the specific language governing permissions and
134limitations under the License.
135```
136
137[compose]: https://developer.android.com/jetpack/compose
138[mdc]: https://github.com/material-components/material-components-android
139[mdc3theme]: ../api/themeadapter-material3/com.google.accompanist.themeadapter.material3/-mdc-3-theme.html
140[createmdc3theme]: ../api/themeadapter-material3/com.google.accompanist.themeadapter.material3/create-mdc-3-theme.html
141[materialtheme]: https://developer.android.com/reference/kotlin/androidx/compose/material3/MaterialTheme
142[colorscheme]: https://developer.android.com/reference/kotlin/androidx/compose/material3/ColorScheme
143[typography]: https://developer.android.com/reference/kotlin/androidx/compose/material3/Typography
144[shapes]: https://developer.android.com/reference/kotlin/androidx/compose/material3/Shapes
145