views:

1107

answers:

2

In the ApiDemos, there is a view example called Gallery1 which declares a custom style in attrs.xml, as such:

<declare-styleable name="Gallery1">
    <attr name="android:galleryItemBackground" />
</declare-styleable>

now, I want to do the same thing for my widgets, but using a different namespace. However, as soon as I replace the android: namespace with something else, I get this error:

ERROR: In Gallery1, unable to find attribute myns:galleryItemBackground

Unable to find attribute? Why does it look for an attribute I am about to declare? Isn't the point of this file to be able to name your own custom attributes?

It's interesting to note that it works if you do not supply a custom namespace, but just an attribute name.

A: 

I found this article helpful in similar situation.

"Referring to our new attributes is actually a two step process. First we declared a new namespace and next we specified the values of our new attributes in the XML usage."

bhatt4982
I've read that article, but it's outdated and wrong (it simply doesn't work that way, at least not anymore).
Matthias
The article works fine with Android 1.6 [tested]. But I got your question wrong. You wont be able to specify any namespace while declaring styleable, it is already in your app namespace. While you or someone else need to use the attrs in Layout XMLs, they need to use it with your app namespace. As we use android specified attrs with android:attr
bhatt4982
I think the problem may be related to me referencing those styles from another project. That is, I declare attrs.xml in project A, create a JAR from it, and try to access them in another project B. I always get "no resource identifier found for attribute X". It works when not using styled attributes, but plain attributes.
Matthias
Note: class R from project A is exported in the JAR, and it includes the stylable attribute IDs. No idea why B can't see it.
Matthias
A: 

I had a similar problem resulting in the error message No resource identifier found for attribute in package

The solution for me was to declare the namespace of the application (as declared in the AndroidManifest.xml) instead of the namespace where your custom View (Gallery1) resides, when you use the custom attribute.

so in your xml file where you use your custom attribute you have to specify:

xmlns:myns="http://schemas.android.com/apk/res/my.application.package.name"

...

<gallery.widget.package.Gallery1


myns:myCustomAttr="xxx"
/>
pivotnig