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

Viewed 47514

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?

7 Answers

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.

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.

Related