R.styleable can not be resolved, why?

Viewed 44398

I have a resources.xml file located under direcotry values/ , That's

/values/resources.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="TheMissingTabWidget"> 
        <attr name="android:divider" /> 
    </declare-styleable> 
</resources>

In my java code, when I try to access this resource by R.styleable.TheMissingTabWidget , eclipse complain that styleable cannot be resolved or is not a field. Why? Why I can not access this resource? (I am using android 2.1-updated).

8 Answers

In my case I had inadvertently done import android.R instead of import com.<mypackage>.R.

Replace <mypackage> with your package name (or just delete the current import and let Android Studio do the rest).

You can access your package level stylable like this

<yourpackagename>.R.styleable.name

I had an undefined styleable error showing in Android Studio, but then I noticed the build was successful. I did Invalidate Caches & Restart and the problem went away.
(It took me far too long to figure it out.)

Simply be sure use :

import com.<your-package>.R

not:

import android.R

Copy/paste of code in a file might add imports on the fly. Check your import statements.

The following needs to be removed.

import android.R

File names like attrs.xml, colors.xml do not matter. This is only for organizing better.

Entry has to be in a xml file under folder values (folder name matters) For convention lets keep attrs.xml

Any of the following entries are OK. You do not need a fully qualified class name.

  <declare-styleable name="com.mycom.app.MyClass">
        <attr name="isSelected" format="boolean"/>
    </declare-styleable>

  <declare-styleable name="MyClass">
        <attr name="isSelected" format="boolean"/>
  </declare-styleable>

In fact, a fully qualified name will be a bit of extra pain. Will make the resource name too long when you access it in class.

Related