0

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.

1
  • 1
    How exactly you want the layout to be ? and also what have you tried so far ? Commented Jun 30, 2014 at 9:54

2 Answers 2

1

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));

Sign up to request clarification or add additional context in comments.

2 Comments

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..
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[].
0

You might use also the TableLayout and set android:layout_width="match_parent" and android:layout_height="match_parent"to image to fill the cell and after manage your cells and rows as you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.