Android Hello, Gallery tutorial -- "R.styleable cannot be resolved"
Asked Answered
F

7

37

When working on the Hello, Gallery tutorial/sample app, after following the instructions on the site, Eclipse reported that R.styleable cannot be resolved.

What is the reason for this error, and how can it be fixed or worked around?

Fractious answered 11/11, 2009 at 19:29 Comment(1)
Make sure you explicitly import your R classTriaxial
F
69

Per this thread, R.styleable has been removed from android 1.5 and higher.

There are a number of ways to get the sample to work, the simplest that I found was recommended by Justin Anderson in the thread linked to above:

  1. Create a new XML file called "resources.xml" with the following content:

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <declare-styleable name="Gallery1"> 
            <attr name="android:galleryItemBackground" /> 
        </declare-styleable> 
    </resources>
    
  2. Place the XML file in the res\values directory (alongside strings.xml)

  3. Update the constructor for your ImageAdapter with the following (assuming the ImageAdapter class is defined in its own file):

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }
    

This solution basically defines the styleable attribute as a resource of the app itself and gives it the necessary structure to work in the app. Note that the app can run fine if you just omit the two lines of code (prior to a.recycle();), all this code does is set a grey background around the images in the Gallery.

Fractious answered 11/11, 2009 at 19:36 Comment(5)
I think a few thousand people must have had this issue.Mandymandych
Yeah, I had to dig for a while before I found a straightforward and workable solution, so I thought I'd post it here on SO to make it easier for folks to find.Fractious
Thanks for this. I wish they'd update the tutorial! By the way -- I needed to name the resources child element declare-styleable, i.e. styleable not stylable.Frunze
There's a little typo in your answer. In the XML it's declare-styleable instead of declare-stylable.Intendancy
Your question,Your answer. Good explanation.Younglove
E
12

The reason for this problem is the resources they tell you to put into res/values/attrs.xml are:

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

But then you get this adapter, which Eclipse can't figure out and frankly makes no sense:

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
    mGalleryItemBackground = a.getResourceId(
            android.R.styleable.Theme_galleryItemBackground, 0);
    a.recycle();
}

That's because you shouldn't have "android." preceeding the resources, the styleable name is Theme here but HelloGallery in the actual resource, and the galleryItemBackground puts android between the styleable name and the attribute like this: Theme_android_galleryItemBackground

So if want the ImageAdapter method to work with the resources you're given, you should rewrite it like this:

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
    mGalleryItemBackground = a.getResourceId(
            R.styleable.HelloGallery_android_galleryItemBackground, 0);
    a.recycle();
}

For future problems regarding resources (R.* cannot be resolved type errors), examine /gen/R.java for what the resources are actually being named.

Electrophilic answered 6/5, 2010 at 7:41 Comment(0)
L
6

I have the same problem and I found in the custom view sample code (PieChart) of Google

import com.example.android.customviews.R;

when i comment that import line, Eclipse will notice error: "R cannot to be resolved to a variable". so you should make an import of your package similar statement above. Ex:

import your.package.name.R;

it fixes similar error for other projects of mine

Latticed answered 8/6, 2014 at 8:36 Comment(0)
H
5

A slightly easier, and certainly more MVCish way is to use the style system:

If you don't have a theme yet, create a styles.xml under res/values. In it, you should have:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="GalleryItem">
        <item name="android:background">?android:attr/galleryItemBackground</item>
    </style>
</resources>

This will define a new style which we are calling GalleryItem and setting the background resource of whatever the style gets applied to, to the value of the style attribute android:attr/galleryItemBackground (you can see a lot of examples of this being done in the frameworks/base/core/res/res/values/themes.xml in Android's source).

Then in an XML declaration for an ImageView, you can simply apply your GalleryItem style by adding style="@style/GalleryItem", eg:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/icon"
  android:scaleType="fitXY"
  android:layout_width="136dip"
  android:layout_height="88dip"
  style="@style/GalleryItem"
/>

This will keep your style stuff out of your adapter code (which is good!) and allow for more generic adapters that don't need to care how you're visualizing your data.

Hautevienne answered 9/12, 2010 at 17:17 Comment(3)
How do you hook the ImageView into the gallery though? The gallery comes without any inside elements that are declared in the XML.Embroil
Yeah, galleries work through Adapters, which provide the views. You'll need to create an adapter that inflates the ImageView from xml. See ApiDemos/src/com/example/android/apis/view/Gallery2.java for an example.Hautevienne
Yes, this approach is much cleaner, even just for the fact of using a xml-declared ImageView, as opposed to the code-created oneKlee
T
3

styleable is not supported http://developer.android.com/sdk/RELEASENOTES.html

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 <declare-styleable> 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.

Tufthunter answered 11/4, 2012 at 18:45 Comment(0)
R
1

There are have a little error in the select answer,instead stylable with styleable

It's should like this:

<declare-styleable name="Gallery1"> 
    <attr name="android:galleryItemBackground" /> 
</declare-styleable> 
Resnatron answered 17/5, 2010 at 14:59 Comment(0)
A
1

I tried everything, but with no luck. The generated R.java was showing the stylable class but the compilation was showing "Stylable Not found". I just added the package name before R, after the change, everything is working fine now...

So, if your package name is com.example.test, then modify following code...

public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();

}

TO

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = c.obtainStyledAttributes(com.example.test.R.styleable.Gallery1);
    mGalleryItemBackground = a.getResourceId(com.example.test.R.styleable.Gallery1_android_galleryItemBackground, 0);
    a.recycle();
}
Affianced answered 25/3, 2014 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.