Reputation: 121
hi am doing one app here i need to display images horiznatally.so using gallery i displyed images.but i need different background to each image in gallery.i dont know how to do. i gave background to gallery but single same background coming entire gallery.but i need diffrent background to each image.any one help me.i treid using below code.
example .class:
public class example extends Activity {
/** Called when the activity is first created. */
private Gallery gallery;
private ImageView imgView;
private Integer[] Imgid = {
R.drawable.popup2, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
Upvotes: 0
Views: 322
Reputation: 1403
You have to use getView method of adapter to set background image of different Imageview // Storing background Image Integer []backGroundImage ={ R.drawable.background1, R.drawable.background2, R.drawable.background3 };
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(cont);
-----
-----
----
// setback ground image imageView.setBackgroundResource(backGroundImage[count]); }
Upvotes: 0
Reputation: 1182
Check out this example of an ImageAdapter. You should have some sorf of list with the references to the images in drawables and set them inside the getView method of the adapter using imageView.setImageResource(mImageIds[position]);
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
Upvotes: 1
Reputation: 5819
I think the GalItemBg
variable is initialized in constructor of your adapter class, it is not changed when getView
method is called, so you got the same background.
try
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(Imgid[position]);
return imgView;
}
Upvotes: 0