Android Hello, Gallery tutorial -- R.styleable cannot be resolved
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:
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>Place the XML file in the res\values directory (alongside strings.xml)
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.
import .R cannot be resolved when I import actionbarsherlock
edited the build path of my android project to include the library
Never manually modify the build path of an Android project.
ActionBarSherlock is an Android library project. You need to add a reference to ActionBarSherlock's library project from your main application project and undo the manual change to the build path.
UPDATE
The second half of your problem was that your build target was set too low. The build target controls what version of the Android classes, resources, etc. is available to your app, and if you use ActionBarSherlock, you need this to be API Level 14 or higher.
if I make my project build target 4.0, can I still run it on an emulator/phone running Android 2.3?
Yes. Set your android:minSdkVersion
to be 10 or lower. Eclipse (via Lint) will yell at you if you accidentally try using classes, methods, and such that are higher than your minSdkVersion
, even though they are legal due to your build target.
How do I customize the look & feel of a Gallery?
Ok... this thread answers the actual question:
Android Hello, Gallery tutorial -- "R.styleable cannot be resolved"
Hope it helps someone.
Difference between declare-styleable and style
I think there is only the following difference between declaring an attribute as styleable or not.
In attrs.xml you can declare custom attributes, either directly within the "resources" section or within "declare-styleable":
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="attrib1" format="string" />
<declare-styleable name="blahblah">
<attr name="attrib2" format="string" />
</declare-styleable>
So now we defined "attrib1" as non-styleable and "attrib2" as styleable.
In layout/someactivity.xml
we can use these attributes directly (no namespace needed):
<com.custom.ViewClass attrib1="xyz" attrib2="abc"/>
You can use the "styleable" attribute "attrib2" within a style.xml
declaration. Again, no namespace is needed here (even if a namespace was used in the layout XML).
<style name="customstyle" parent="@android:style/Widget.TextView">
<item name="attrib2">text value</item>
<!-- customize other, standard attributes too: -->
<item name="android:textColor">@color/white</item>
</style>
Then you can also set the attributes per style.
<com.custom.CustomView attrib1="xyz" style="@style/customstyle"/>
Let us assume that we do this: we set attrib1
directly in XML, and we set attrib2
in a style.
Elsewhere I have seen descriptions stating that "blahblah
" must be the name of the custom view class that uses these attributes, and that you need to use a namespace to refer to your custom attributes in the layout XML. But none of this seems to be necessary.
The difference between styleable and non-styleable seems to be that:
- You can use styleable attributes in "
style.xml
" declarations. - The constructor of the custom class needs to read the styled and the non-styled attributes in a different way: the styled attributes with
obtainStyledAttributes()
, and the non-styled attributes withattr.getAttributeValue()
or similar.
In most tutorials and examples I've seen on the Web, only the obtainStyledAttributes()
was used. However, this does not work with attributes declared directly in layout, without using a style. If you do obtainStyledAttributes()
as shown in most tutorials, you will not get the attribute attrib1
at all; you will only get attrib2
since it was declared in the style. The direct method using attr.getAttributeValue()
works:
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
String attrib1 = attrs.getAttributeValue(null, "attrib1");
// do something with this value
}
Since we used no namespace to declare "attrib1
", we pass null
as the namespace argument in getAttributeValue()
.
Related Topics
Scaled Bitmap Maintaining Aspect Ratio
Adding Tab Inside Fragment in Android
Create Custom Big Notifications
Getting Results of Nearby Places from User's Location Using Google Maps API in Android
How to Use Recyclerview.Scrolltoposition() to Move the Position to the Top of Current View
How to Be Notified on Wifi Network Status Change
How to Set Notification with Custom Sound in Android
How to Block Virtual Keyboard While Clicking on Edittext in Android
Retrieve Large Blob from Android SQLite Database
Android - Preserve or Delete Files Created by the Application on Uninstall
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"
How to Get Add to Home Screen Pop Up on Site Open in Mobile Browser
How to Save File from Website to Sdcard
Phonegap Plugin:How to Convert Base64 String to a Png Image in Android
Rate Google Play Application Directly in App
Android Ndk C++ Jni (No Implementation Found for Native...)