xref: /aosp_15_r20/external/accompanist/docs/themeadapter-material.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1*fa44fe6aSInna Palant# Material Theme Adapter
2*fa44fe6aSInna Palant
3*fa44fe6aSInna Palant[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-themeadapter-material)](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.** 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 Palant
15*fa44fe6aSInna PalantA library that enables the reuse of [MDC-Android][mdc] Material 2 XML themes, for theming in [Jetpack Compose][compose].
16*fa44fe6aSInna Palant
17*fa44fe6aSInna Palant![Material Theme Adapter header](themeadapter/material-header.png)
18*fa44fe6aSInna Palant
19*fa44fe6aSInna PalantThe basis of Material Design 2 theming in [Jetpack Compose][compose] is the [`MaterialTheme`][materialtheme] composable, where you provide [`Colors`][colors], [`Typography`][typography] and [`Shapes`][shapes] instances containing your styling parameters:
20*fa44fe6aSInna Palant
21*fa44fe6aSInna Palant``` kotlin
22*fa44fe6aSInna PalantMaterialTheme(
23*fa44fe6aSInna Palant    colors = colors,
24*fa44fe6aSInna Palant    typography = type,
25*fa44fe6aSInna Palant    shapes = shapes
26*fa44fe6aSInna Palant) {
27*fa44fe6aSInna Palant    // M2 Surface, Scaffold, etc.
28*fa44fe6aSInna Palant}
29*fa44fe6aSInna Palant```
30*fa44fe6aSInna Palant
31*fa44fe6aSInna Palant[Material Components for Android][mdc] themes allow for similar theming for views via XML theme attributes, like so:
32*fa44fe6aSInna Palant
33*fa44fe6aSInna Palant``` xml
34*fa44fe6aSInna Palant<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
35*fa44fe6aSInna Palant    <!-- Material 2 color attributes -->
36*fa44fe6aSInna Palant    <item name="colorPrimary">@color/purple_500</item>
37*fa44fe6aSInna Palant    <item name="colorSecondary">@color/green_200</item>
38*fa44fe6aSInna Palant
39*fa44fe6aSInna Palant    <!-- Material 2 type attributes-->
40*fa44fe6aSInna Palant    <item name="textAppearanceBody1">@style/TextAppearance.MyApp.Body1</item>
41*fa44fe6aSInna Palant    <item name="textAppearanceBody2">@style/TextAppearance.MyApp.Body2</item>
42*fa44fe6aSInna Palant
43*fa44fe6aSInna Palant    <!-- Material 2 shape attributes-->
44*fa44fe6aSInna Palant    <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.MyApp.SmallComponent</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] M2 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 PalantMdcTheme {
53*fa44fe6aSInna Palant    // MaterialTheme.colors, 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!!! caution
61*fa44fe6aSInna Palant    If you are using an AppCompat (i.e. non-MDC) theme in your app, you should use
62*fa44fe6aSInna Palant    [AppCompat Theme Adapter](https://github.com/google/accompanist/tree/main/themeadapter-appcompat)
63*fa44fe6aSInna Palant    instead, as it attempts to bridge the gap between [AppCompat][appcompat] XML themes, and M2 themes in [Jetpack Compose][compose].
64*fa44fe6aSInna Palant
65*fa44fe6aSInna Palant### Customizing the M2 theme
66*fa44fe6aSInna Palant
67*fa44fe6aSInna PalantThe [`MdcTheme()`][mdctheme] 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 [`createMdcTheme()`][createmdctheme] function:
68*fa44fe6aSInna Palant
69*fa44fe6aSInna Palant``` kotlin
70*fa44fe6aSInna Palantval context = LocalContext.current
71*fa44fe6aSInna Palantval layoutDirection = LocalLayoutDirection.current
72*fa44fe6aSInna Palantvar (colors, typography, shapes) = createMdcTheme(
73*fa44fe6aSInna Palant    context = context,
74*fa44fe6aSInna Palant    layoutDirection = layoutDirection
75*fa44fe6aSInna Palant)
76*fa44fe6aSInna Palant
77*fa44fe6aSInna Palant// Modify colors, typography or shapes as required.
78*fa44fe6aSInna Palant// Then pass them through to MaterialTheme...
79*fa44fe6aSInna Palant
80*fa44fe6aSInna PalantMaterialTheme(
81*fa44fe6aSInna Palant    colors = colors ?: MaterialTheme.colors,
82*fa44fe6aSInna Palant    typography = typography ?: MaterialTheme.typography,
83*fa44fe6aSInna Palant    shapes = shapes ?: MaterialTheme.shapes
84*fa44fe6aSInna Palant) {
85*fa44fe6aSInna Palant    // Rest of M2 layout
86*fa44fe6aSInna Palant}
87*fa44fe6aSInna Palant```
88*fa44fe6aSInna Palant
89*fa44fe6aSInna Palant### Limitations
90*fa44fe6aSInna Palant
91*fa44fe6aSInna PalantThere are some known limitations with the implementation at the moment:
92*fa44fe6aSInna Palant
93*fa44fe6aSInna Palant* This relies on your `Activity`/`Context` theme extending one of the `Theme.MaterialComponents` themes.
94*fa44fe6aSInna Palant* Text colors are not read from the text appearances by default. You can enable it via the `setTextColors` function parameter.
95*fa44fe6aSInna Palant* Variable fonts are not supported in Compose yet, meaning that the value of `android:fontVariationSettings` are currently ignored.
96*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.
97*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.
98*fa44fe6aSInna Palant
99*fa44fe6aSInna Palant---
100*fa44fe6aSInna Palant
101*fa44fe6aSInna Palant## Usage
102*fa44fe6aSInna Palant
103*fa44fe6aSInna Palant[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-themeadapter-material)](https://search.maven.org/search?q=g:com.google.accompanist)
104*fa44fe6aSInna Palant
105*fa44fe6aSInna Palant``` groovy
106*fa44fe6aSInna Palantrepositories {
107*fa44fe6aSInna Palant    mavenCentral()
108*fa44fe6aSInna Palant}
109*fa44fe6aSInna Palant
110*fa44fe6aSInna Palantdependencies {
111*fa44fe6aSInna Palant    implementation "com.google.accompanist:accompanist-themeadapter-material:<version>"
112*fa44fe6aSInna Palant}
113*fa44fe6aSInna Palant```
114*fa44fe6aSInna Palant
115*fa44fe6aSInna Palant### Library Snapshots
116*fa44fe6aSInna Palant
117*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.
118*fa44fe6aSInna Palant
119*fa44fe6aSInna Palant---
120*fa44fe6aSInna Palant
121*fa44fe6aSInna Palant## Contributions
122*fa44fe6aSInna Palant
123*fa44fe6aSInna PalantPlease contribute! We will gladly review any pull requests.
124*fa44fe6aSInna PalantMake sure to read the [Contributing](../contributing) page first though.
125*fa44fe6aSInna Palant
126*fa44fe6aSInna Palant## License
127*fa44fe6aSInna Palant
128*fa44fe6aSInna Palant```
129*fa44fe6aSInna PalantCopyright 2022 The Android Open Source Project
130*fa44fe6aSInna Palant
131*fa44fe6aSInna PalantLicensed under the Apache License, Version 2.0 (the "License");
132*fa44fe6aSInna Palantyou may not use this file except in compliance with the License.
133*fa44fe6aSInna PalantYou may obtain a copy of the License at
134*fa44fe6aSInna Palant
135*fa44fe6aSInna Palant    https://www.apache.org/licenses/LICENSE-2.0
136*fa44fe6aSInna Palant
137*fa44fe6aSInna PalantUnless required by applicable law or agreed to in writing, software
138*fa44fe6aSInna Palantdistributed under the License is distributed on an "AS IS" BASIS,
139*fa44fe6aSInna PalantWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
140*fa44fe6aSInna PalantSee the License for the specific language governing permissions and
141*fa44fe6aSInna Palantlimitations under the License.
142*fa44fe6aSInna Palant```
143*fa44fe6aSInna Palant
144*fa44fe6aSInna Palant[compose]: https://developer.android.com/jetpack/compose
145*fa44fe6aSInna Palant[mdc]: https://github.com/material-components/material-components-android
146*fa44fe6aSInna Palant[mdctheme]: ../api/themeadapter-material/com.google.accompanist.themeadapter.material/-mdc-theme.html
147*fa44fe6aSInna Palant[createmdctheme]: ../api/themeadapter-material/com.google.accompanist.themeadapter.material/create-mdc-theme.html
148*fa44fe6aSInna Palant[materialtheme]: https://developer.android.com/reference/kotlin/androidx/compose/material/MaterialTheme
149*fa44fe6aSInna Palant[colors]: https://developer.android.com/reference/kotlin/androidx/compose/material/Colors
150*fa44fe6aSInna Palant[typography]: https://developer.android.com/reference/kotlin/androidx/compose/material/Typography
151*fa44fe6aSInna Palant[shapes]: https://developer.android.com/reference/kotlin/androidx/compose/material/Shapes
152