Home > Blockchain >  Studio's demo project fails when extending a Material3 style
Studio's demo project fails when extending a Material3 style

Time:10-26

I've created a dead simple Android Studio's starter project (Basic Activity) and I'm getting linkage errors after adding a simple style file in res/values/app_styles.xml:

<resources>
    <style name="MyFab" parent="Widget.MaterialComponents.ExtendedFloatingActionButton">
        <item name="iconTint">?attr/colorOnBackground</item>
        <item name="app:fabSize">mini</item>
        <item name="app:tint">?attr/colorOnPrimary</item>
    </style>
</resources>

The app namespace seems to be unknown to Android Studio, every app:** item is highlighted in red, and build fails with linkage errors:

Android resource linking failed
xxx.myapp.app-mergeDebugResources-43:/values-w600dp-v13/values-w600dp-v13.xml:6: error: style attribute 'app:attr/fabSize' not found.
xxx.myapp.app-mergeDebugResources-43:/values-w600dp-v13/values-w600dp-v13.xml:7: error: style attribute 'app:attr/tint' not found.
error: failed linking references.

What am I doing wrong here? Should I use a different file name/location?

PS: Android Studio Dolphin | 2021.3.1 Patch 1 on Mac x64, fresh install.

Project dependencies:

dependencies {
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

CodePudding user response:

You have to remove the app prefix in the attribute name in the style definition.

Pleas note that ExtendedFloatingActionButton is a child class of MaterialButton, rather than FloatingActionButton. This means that several attributes which are applicable to FloatingActionButton have different naming in ExtendedFloatingActionButton or don't exist.

fabSize and tint don't exist for the ExtendedFloatingActionButton.

<style name="MyFab" parent="Widget.MaterialComponents.FloatingActionButton">
    <item name="iconTint">?attr/colorOnBackground</item>
    <item name="fabSize">mini</item>
    <item name="tint">?attr/colorOnPrimary</item>
</style>
  • Related