24

I'm a using a view flow component into my project, which allows the dev to override some attributes, like that :

<org.taptwo.android.widget.TitleFlowIndicator
    ...
    app:footerTriangleHeight="0dip"
    ... />

I am reusing this component into several layouts, and I would like to put the properties into a style. But when I'm doing this, the parser says Error: No resource found that matches the given name: attr 'app:footerTriangleHeight', even if I add the namespace in the styles file.

Is there a way to do that in android ?

Thanks.

Matthieu Oger
  • 823
  • 1
  • 11
  • 20
  • Selected answer is wrong/incomplete. You can style custom UI elements in the style file, done this myself... See my answer for example – Kurru Aug 29 '11 at 13:20
  • please check [here](https://stackoverflow.com/questions/6860886/custom-attributes-in-styles-xml) that will address your issue. – aolphn Jul 04 '18 at 06:33
  • I found the same issue on SO and that addressed my problem,[here.](https://stackoverflow.com/questions/6860886/custom-attributes-in-styles-xml) – aolphn Jul 04 '18 at 06:35
  • [I found the same issue in here](https://stackoverflow.com/questions/6860886/custom-attributes-in-styles-xml) – aolphn Jul 04 '18 at 06:36

2 Answers2

40

Android Application

If you are not using Android Libraries, then here is what you can do:

  1. Define custom styleable attribute (I guess you've already done that).
  2. Do not use your namespace prefix in style items (namespace defaults to current app's namespace).

Example:

In attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="testAttr" format="string"/>        
</resources>

In styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>        
    <style name="TestStyle" >
      <item name="testAttr">asdf</item>
    </style>        
</resources>

Android Library

If custom attribute comes from Android Library, you can still use described approach. It theoretically should work, because Android Library's namespace is the same as application's (from aapt tool perspective during the build). But I haven't test this myself.


If you're specifying namespace, it will show error. As far as I know, styles do not support xml namespaces. So this will fail:

In styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:app="http://schemas.android.com/apk/res/app.package.name">        
    <style name="TestStyle" >
      <item name="app:testAttr">asdf</item>
    </style>        
</resources>

Parser automatically defaults to current app's AndroidManifest namespace, though you can not specify this namespace explicitly.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • 2
    You are right, I just needed to remove the namespace in the styles file, and it worked. But it's weird that it is needed in a normal layout file, and not in a resources file. Thanks. – Matthieu Oger Aug 29 '11 at 13:17
  • This works with the new CardView widget for example: just remove the namespace, and a style item like 16dp compiles and runs fine. – personne3000 Nov 21 '14 at 12:58
6

You can define the style in terms of your namespace, i've used it myself

i.e

<style name="whatever">
    <item name="YourPackage.YourSubPackage:parametername">@drawable_or_anything/bla</item>
</style>
Kurru
  • 14,180
  • 18
  • 64
  • 84