R.styleable can not be resolved, why?
Asked Answered
C

8

31

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).

Cisneros answered 13/7, 2011 at 7:19 Comment(2)
possible duplicate of Android Hello, Gallery tutorial -- "R.styleable cannot be resolved"Sansom
If you still have problem with it - check which R you are using. You must use your project R classSentinel
I
23

plz make values/attrs.xml resources like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="**com.admob.android.ads.AdView**"><--- where u want to use
       <attr name="backgroundColor" format="color" />
       <attr name="TextColor" format="color" />
       <attr name="keywords" format="string" />
       <attr name="refreshInterval" format="integer" />
    </declare-styleable>
</resources>
Inept answered 13/7, 2011 at 7:28 Comment(0)
H
17

According to the SDK Release Notes,

The android.R.styleable class and its fields were removed from the public API, to better ensure forward-compatibility for applications. The constants declared in android.R.styleable were platform-specific and subject to arbitrary change across versions, so were not suitable for use by applications. You can still access the platform's styleable attributes from your resources or code. To do so, declare a custom resource element using a in your project's res/values/R.attrs file, then declare the attribute inside. For examples, see "sdk"/samples/ApiDemos/res/values/attrs.xml. For more information about custom resources, see Custom Layout Resources. Note that the android.R.styleable documentation is still provided in the SDK, but only as a reference of the platform's styleable attributes for the various elements.

Have a look to the ApiDemos code and the file res/values/attrs.xml

Hassan answered 13/7, 2011 at 8:1 Comment(1)
This doesn't explain how you put R.styleable into attrs file.Hardheaded
A
14

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).

Aubert answered 13/5, 2020 at 19:5 Comment(2)
This is the answer that saved me. It should have more upvotes than that other answer with over 15 upvotes that isn't even an answer.Vasques
This should be the answerSerpasil
L
5

You can access your package level stylable like this

<yourpackagename>.R.styleable.name
Lime answered 11/2, 2019 at 6:31 Comment(0)
B
3

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.)

Beanfeast answered 24/4, 2020 at 17:34 Comment(0)
P
2

Simply be sure use :

import com.<your-package>.R

not:

import android.R
Pessa answered 27/1, 2021 at 6:25 Comment(0)
H
0

What you need to do is declare your styleable in attrs.xml, not resources.xml. Then you'll be able to refer to it from your code like this:

R.styleable.TheMissingTabWidget
Hardheaded answered 5/1, 2016 at 16:50 Comment(1)
file names like attrs.xml, resources.xml do not matter. All contain resource tags as root. Children of Resorce tag matter. All can be in a single, if you do not want to organize.Cowpox
C
0

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.

Cowpox answered 31/5, 2022 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.