xref: /aosp_15_r20/external/accompanist/docs/themeadapter-material3.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1*fa44fe6aSInna Palant# Material 3 Theme Adapter
2*fa44fe6aSInna Palant
3*fa44fe6aSInna Palant[![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*fa44fe6aSInna Palant
5*fa44fe6aSInna Palant!!! warning
6*fa44fe6aSInna Palant    **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*fa44fe6aSInna Palant
8*fa44fe6aSInna Palant## Migration
9*fa44fe6aSInna PalantRecommendation: 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*fa44fe6aSInna Palant
11*fa44fe6aSInna PalantYou 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*fa44fe6aSInna Palant
13*fa44fe6aSInna Palant## Original Documenation
14*fa44fe6aSInna PalantA library that enables the reuse of [MDC-Android][mdc] Material 3 XML themes, for theming in [Jetpack Compose][compose].
15*fa44fe6aSInna Palant
16*fa44fe6aSInna Palant![Material 3 Theme Adapter header](themeadapter/material3-header.png)
17*fa44fe6aSInna Palant
18*fa44fe6aSInna PalantThe 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*fa44fe6aSInna Palant
20*fa44fe6aSInna Palant``` kotlin
21*fa44fe6aSInna PalantMaterialTheme(
22*fa44fe6aSInna Palant    colorScheme = colorScheme,
23*fa44fe6aSInna Palant    typography = typography,
24*fa44fe6aSInna Palant    shapes = shapes
25*fa44fe6aSInna Palant) {
26*fa44fe6aSInna Palant    // M3 Surface, Scaffold, etc.
27*fa44fe6aSInna Palant}
28*fa44fe6aSInna Palant```
29*fa44fe6aSInna Palant
30*fa44fe6aSInna Palant[Material Components for Android][mdc] themes allow for similar theming for views via XML theme attributes, like so:
31*fa44fe6aSInna Palant
32*fa44fe6aSInna Palant``` xml
33*fa44fe6aSInna Palant<style name="Theme.MyApp" parent="Theme.Material3.DayNight">
34*fa44fe6aSInna Palant    <!-- Material 3 color attributes -->
35*fa44fe6aSInna Palant    <item name="colorPrimary">@color/purple_500</item>
36*fa44fe6aSInna Palant    <item name="colorSecondary">@color/purple_700</item>
37*fa44fe6aSInna Palant    <item name="colorTertiary">@color/green_200</item>
38*fa44fe6aSInna Palant
39*fa44fe6aSInna Palant    <!-- Material 3 type attributes-->
40*fa44fe6aSInna Palant    <item name="textAppearanceBodyLarge">@style/TextAppearance.MyApp.BodyLarge</item>
41*fa44fe6aSInna Palant    <item name="textAppearanceBodyMedium">@style/TextAppearance.MyApp.BodyMedium</item>
42*fa44fe6aSInna Palant
43*fa44fe6aSInna Palant    <!-- Material 3 shape attributes-->
44*fa44fe6aSInna Palant    <item name="shapeAppearanceCornerSmall">@style/ShapeAppearance.MyApp.CornerSmall</item>
45*fa44fe6aSInna Palant</style>
46*fa44fe6aSInna Palant```
47*fa44fe6aSInna Palant
48*fa44fe6aSInna PalantThis 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*fa44fe6aSInna Palant
50*fa44fe6aSInna Palant
51*fa44fe6aSInna Palant``` kotlin
52*fa44fe6aSInna PalantMdc3Theme {
53*fa44fe6aSInna Palant    // MaterialTheme.colorScheme, MaterialTheme.typography, MaterialTheme.shapes
54*fa44fe6aSInna Palant    // will now contain copies of the Context's theme
55*fa44fe6aSInna Palant}
56*fa44fe6aSInna Palant```
57*fa44fe6aSInna Palant
58*fa44fe6aSInna PalantThis is especially handy when you're migrating an existing app, a `Fragment` (or other UI container) at a time.
59*fa44fe6aSInna Palant
60*fa44fe6aSInna Palant### Customizing the M3 theme
61*fa44fe6aSInna Palant
62*fa44fe6aSInna PalantThe [`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*fa44fe6aSInna Palant
64*fa44fe6aSInna Palant``` kotlin
65*fa44fe6aSInna Palantval context = LocalContext.current
66*fa44fe6aSInna Palantvar (colorScheme, typography, shapes) = createMdc3Theme(
67*fa44fe6aSInna Palant    context = context
68*fa44fe6aSInna Palant)
69*fa44fe6aSInna Palant
70*fa44fe6aSInna Palant// Modify colorScheme, typography or shapes as required.
71*fa44fe6aSInna Palant// Then pass them through to MaterialTheme...
72*fa44fe6aSInna Palant
73*fa44fe6aSInna PalantMaterialTheme(
74*fa44fe6aSInna Palant    colorScheme = colorScheme,
75*fa44fe6aSInna Palant    typography = typography,
76*fa44fe6aSInna Palant    shapes = shapes
77*fa44fe6aSInna Palant) {
78*fa44fe6aSInna Palant    // Rest of M3 layout
79*fa44fe6aSInna Palant}
80*fa44fe6aSInna Palant```
81*fa44fe6aSInna Palant
82*fa44fe6aSInna Palant### Limitations
83*fa44fe6aSInna Palant
84*fa44fe6aSInna PalantThere are some known limitations with the implementation at the moment:
85*fa44fe6aSInna Palant
86*fa44fe6aSInna Palant* This relies on your `Activity`/`Context` theme extending one of the `Theme.Material3` themes.
87*fa44fe6aSInna Palant* Text colors are not read from the text appearances by default. You can enable it via the `setTextColors` function parameter.
88*fa44fe6aSInna Palant* Variable fonts are not supported in Compose yet, meaning that the value of `android:fontVariationSettings` are currently ignored.
89*fa44fe6aSInna Palant* 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*fa44fe6aSInna Palant* 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*fa44fe6aSInna Palant
92*fa44fe6aSInna Palant---
93*fa44fe6aSInna Palant
94*fa44fe6aSInna Palant## Usage
95*fa44fe6aSInna Palant
96*fa44fe6aSInna Palant[![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*fa44fe6aSInna Palant
98*fa44fe6aSInna Palant``` groovy
99*fa44fe6aSInna Palantrepositories {
100*fa44fe6aSInna Palant    mavenCentral()
101*fa44fe6aSInna Palant}
102*fa44fe6aSInna Palant
103*fa44fe6aSInna Palantdependencies {
104*fa44fe6aSInna Palant    implementation "com.google.accompanist:accompanist-themeadapter-material3:<version>"
105*fa44fe6aSInna Palant}
106*fa44fe6aSInna Palant```
107*fa44fe6aSInna Palant
108*fa44fe6aSInna Palant### Library Snapshots
109*fa44fe6aSInna Palant
110*fa44fe6aSInna PalantSnapshots 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*fa44fe6aSInna Palant
112*fa44fe6aSInna Palant---
113*fa44fe6aSInna Palant
114*fa44fe6aSInna Palant## Contributions
115*fa44fe6aSInna Palant
116*fa44fe6aSInna PalantPlease contribute! We will gladly review any pull requests.
117*fa44fe6aSInna PalantMake sure to read the [Contributing](../contributing) page first though.
118*fa44fe6aSInna Palant
119*fa44fe6aSInna Palant## License
120*fa44fe6aSInna Palant
121*fa44fe6aSInna Palant```
122*fa44fe6aSInna PalantCopyright 2022 The Android Open Source Project
123*fa44fe6aSInna Palant
124*fa44fe6aSInna PalantLicensed under the Apache License, Version 2.0 (the "License");
125*fa44fe6aSInna Palantyou may not use this file except in compliance with the License.
126*fa44fe6aSInna PalantYou may obtain a copy of the License at
127*fa44fe6aSInna Palant
128*fa44fe6aSInna Palant    https://www.apache.org/licenses/LICENSE-2.0
129*fa44fe6aSInna Palant
130*fa44fe6aSInna PalantUnless required by applicable law or agreed to in writing, software
131*fa44fe6aSInna Palantdistributed under the License is distributed on an "AS IS" BASIS,
132*fa44fe6aSInna PalantWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133*fa44fe6aSInna PalantSee the License for the specific language governing permissions and
134*fa44fe6aSInna Palantlimitations under the License.
135*fa44fe6aSInna Palant```
136*fa44fe6aSInna Palant
137*fa44fe6aSInna Palant[compose]: https://developer.android.com/jetpack/compose
138*fa44fe6aSInna Palant[mdc]: https://github.com/material-components/material-components-android
139*fa44fe6aSInna Palant[mdc3theme]: ../api/themeadapter-material3/com.google.accompanist.themeadapter.material3/-mdc-3-theme.html
140*fa44fe6aSInna Palant[createmdc3theme]: ../api/themeadapter-material3/com.google.accompanist.themeadapter.material3/create-mdc-3-theme.html
141*fa44fe6aSInna Palant[materialtheme]: https://developer.android.com/reference/kotlin/androidx/compose/material3/MaterialTheme
142*fa44fe6aSInna Palant[colorscheme]: https://developer.android.com/reference/kotlin/androidx/compose/material3/ColorScheme
143*fa44fe6aSInna Palant[typography]: https://developer.android.com/reference/kotlin/androidx/compose/material3/Typography
144*fa44fe6aSInna Palant[shapes]: https://developer.android.com/reference/kotlin/androidx/compose/material3/Shapes
145