17

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.

mxk
  • 43,056
  • 28
  • 105
  • 132

4 Answers4

22

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 when you use the custom attribute.

In your xml file where you use your custom attribute specify:

xmlns:myns="http://schemas.android.com/apk/res-auto"

...

<gallery.widget.package.Gallery1


myns:myCustomAttr="xxx"
/>
Intrications
  • 16,782
  • 9
  • 50
  • 50
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
8

In case someone is still interested, I had the same problem and solved it by adding a 'format' attribute (it seems it doesn't take 'string' as the default):

<attr name="android:galleryItemBackground" format="integer"/>
Mihai F
  • 161
  • 2
  • 3
1

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
  • 8,024
  • 2
  • 26
  • 18
  • I've read that article, but it's outdated and wrong (it simply doesn't work that way, at least not anymore). – mxk Sep 22 '09 at 10:02
  • 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 Sep 22 '09 at 13:47
  • 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. – mxk Sep 27 '09 at 12:07
  • 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. – mxk Sep 27 '09 at 12:08
1

create a xml file and paste this below code and put in res->values folder

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

and then copy below code

TypedArray typedArray=this.obtainStyledAttributes(R.styleable.Gallery1);
        int back=typedArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        typedArray.recycle();

and set in ur widget background i mean imageView.setBackgroundResource(back);

ilango j
  • 5,967
  • 2
  • 28
  • 25