144

I am going to give an example to demonstrate the greater point.

Imagine my app has a number of FloatingActionButtons. Consequently, I want to create one style and reuse it. So I do the following:

<style name="FabStyle” parent ="Widget.Design.FloatingActionButton">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">16dp</item>
    <item name="app:backgroundTint">@color/accent</item>
    <item name="app:layout_anchorGravity">end|bottom</item>
</style>

The problem I am having is that the code is not compiling because it is complaining about

Error:(40, 5) No resource found that matches the given name: attr 'app:backgroundTint'.

I tried bringing the namespace in through the resources tag but that is not working

<resources
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

Any ideas how I might get this to work?

Nouvel Travay
  • 6,292
  • 13
  • 40
  • 65
  • 1
    Related: https://stackoverflow.com/questions/6860886/custom-attributes-in-styles-xml/6868308 – Tyler Oct 02 '17 at 21:07

2 Answers2

319

For app namespace you don't need to specify app:<property name>. Just <property name> is enough.

For example

<style name="FabStyle" parent="Widget.Design.FloatingActionButton"> 
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">16dp</item>
    <item name="backgroundTint">@color/accent</item>
    <item name="layout_anchorGravity">end|bottom</item>
</style>

And for layout_anchorGravity you need to set it in XML file where you are defining Floating action button.

Tyler
  • 19,113
  • 19
  • 94
  • 151
dex
  • 5,182
  • 1
  • 23
  • 41
  • Awesome! thanks for helping. Minor correction: you can define `layout_anchorGravity` on the style page as well. It works as `end|bottom` – Nouvel Travay Sep 05 '16 at 20:36
  • you saved the day! And the one to be defined locally is `layout_anchor`. +1! – Nouvel Travay Sep 05 '16 at 20:41
  • Well, doing so solved the error mentioned by OP. Even text predictor for `styles.xml` suggests the properties; but somehow it's like these properties are not applied at all after building... I wonder why... – SebasSBM Jun 08 '22 at 17:20
3

You can directly use the property without app

<style name="FabStyle” parent ="Widget.Design.FloatingActionButton">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">16dp</item>
    <item name="backgroundTint">@color/accent</item>
    <item name="layout_anchorGravity">end|bottom</item>
</style>
Maneesh A
  • 41
  • 1