I need to create an image gridview in my android activity. How can it be achieved..I have seen a gridview and used it. Also I have used the image view in my activities. But I donot know how to create a grid view using images. Can someone shed some light regarding this? Any help from anyone is appreciated.. Thanks in advance.
2 Answers
You can customizes the grid view with your own adapter class derived from BaseAdapter. Please use the following code:
Adapter Class:
public class ImageAdapter extends BaseAdapter { private Context mContext;
public Drawable[] images;
public ImageAdapter(Context c,Drawable[] d){
mContext = c;
images=d;
}
public int getCount() {
return images.length;
}
public Object getItem(int position) {
return images[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageDrawable(images[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
Assign this adapter to your grid view as gridView.setAdapter(new ImageAdapter(GridImages.this,images));
2 Comments
njnjnj
I need to add the images to the grid view from a folder in my sd card. How can it be acheived?? Please help me out..
Rajkumar Nagarajan
Please load the images from sdcard using Bitmap Factory as follows Bitmap gridImage = BitmapFactory.decodeFile(new File(Environment.getExternalStorageDirectory(), "file_name"); and please change the adapter constructor parameter Drawable[] to Bitmap[].