Changing Material Components style attributes fails
Asked Answered
V

3

28

I am trying to change the style attributes from the Material Components Theme (like colorPrimaryVariant, colorOnSecondary and so on) but I get the following error:

Android resource linking failed
Output:      C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:3835: error: style attribute 'attr/colorPrimaryVariant (aka com.sample.app:attr/colorPrimaryVariant)' not found.
C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:3836: error: style attribute 'attr/colorOnPrimary (aka com.sample.app:attr/colorOnPrimary)' not found.
C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:3839: error: style attribute 'attr/colorSecondaryVariant (aka com.sample.app:attr/colorSecondaryVariant)' not found.
C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:3840: error: style attribute 'attr/colorOnSecondary (aka com.sample.app:attr/colorOnSecondary)' not found.
error: failed linking references.

This is what my theme looks like:

<resources>

    <!-- Light application theme -->
    <style name="CBTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/cbPrimary</item>
        <item name="colorPrimaryDark">@color/cbPrimaryDark</item>
        <item name="colorPrimaryVariant">@color/cbPrimaryDark</item>
        <item name="colorOnPrimary">#FFFFFF</item>
        <item name="colorAccent">@color/cbAccent</item>
        <item name="colorSecondary">@color/cbAccent</item>
        <item name="colorSecondaryVariant">@color/cbAccentLight</item>
        <item name="colorOnSecondary">#FFFFFF</item>
        <item name="colorError">@color/cbRed</item>

        <item name="android:windowBackground">@color/lightThemeBackground1</item>
    </style>

    [...]

</resources>

If I don't add the four affected attributes everything works fine. My minSdkVersion is 16, compileSdkVersion and targetSdkVersion are 28. I also tried to use the Bridge themes but I get the same error. I double-checked my dependencies and everything seems correct. What am I missing?

Vocalize answered 4/10, 2018 at 20:23 Comment(1)
It seems like they used Theme.AppCompat and if you read it carefully, I think it is available with the AppCompat theme and those are new attributes for the AppCompat to test and not the Theme.MaterialComponents : You can also incrementally test new Material components without changing your app themePlayoff
R
44

It looks like 1.1.0 (currently in alpha) has resolved this issue:

implementation 'com.google.android.material:material:1.1.0-alpha09'
Ronel answered 11/11, 2018 at 10:2 Comment(1)
mvnrepository.com/artifact/com.google.android.material/material tells you how to keep this one warmTernan
Y
2

I have just checked the documentation and the items you used. It seems like it is still not added to the Theme.MaterialComponents nor it doesn't work with that theme.

(got the Not found error (by using Theme.MaterialComponents) too)

The interesting part is, I believe it should work with parent="Theme.AppCompat" since they said it is for the test to see how it works:

You can also incrementally test new Material components without changing your app theme. This allows you to keep your existing layouts looking and behaving the same, while introducing new components to your layout one at a time.

Read: AppCompat Themes

Younts answered 4/10, 2018 at 20:54 Comment(7)
I tried that but unfortunately got the same result. However I found out that the issue only affects the color attributes. All the 'textAppearance' attributes and the 'scrimBackground' attribute mentioned in the documentation work fine (with both 'Theme.MaterialComponents' and 'Theme.AppCompat'). Is it just not possible to change the colors yet?Vocalize
Yes you’re right. All those text attributes work fine in my side too but It seems like those color attributes will be available in a new update or something like that. However, I’m wondering why they introduced those and wrote a documentation for using those on AppCompat and now. It cannot find those attributes. Sounds like a bug or at least, we’ll need an explanation from a reliable source of Google.Playoff
After some looking around I just found this file which is missing the colors while the corresponding file for typography contains all the textAppearance attributes. So I assume you are right and these attributes are not public yet for whatever reason.Vocalize
Well yes. I already check those files which haven’t had those color attributes. That’s why I said it will probably be added in a next future update. :)Playoff
You're right these color attributes don't exist yet, and nothing is using them. It should be released in an alpha version of the library at the end of this month.Arvind
however I'm still having this issue. essentially my app still uses supportlibrary, but using a library using androidxHilliard
So using material.io/develop/android/components/material-button requires 'com.google.android.material:material:1.1.0'? And 1.0.0 of the library only supported the 'old' material button - just solid color or borderless, no custom shaping etc?Zedoary
R
0

colorSurface is used for “sheets” of material (like cards and bottom sheets).

You don’t have to override all colors. Some, such as colorSurface, use neutral colors so relying on the default values is perfectly fine.

Resource : https://material.io/blog/android-material-theme-color

Rubidium answered 3/2, 2023 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.