Hi,
I'm reading the 5th chapter of your book and I'm stuck where you use the declare-styleable XML attribute for the Gallery background... I'm not able to understand how it works and also the java code related to the styleable makes me quite confuse.. I've tried to find some info on the official docs but I could not find anything detailed about styleables. The code is the following:
Code:
<?xml version=â1.0â encoding=âutf-8â?>
<resources>
<declare-styleable name=âGallery1â>
<attr name=âandroid:galleryItemBackgroundâ />
</declare-styleable>
</resources>
Code:
public class GalleryActivity extends Activity {
[...]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
[...]
}
[...]
public class ImageAdapter extends BaseAdapter {
[...]
int itemBackground;
public ImageAdapter(Context c) {
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(
R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
[...]
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
Can you help me to understand why we need to use a styleable and how it works? Thank you very much!