njnjnj
njnjnj

Reputation: 984

Create an image grid view in android

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.

Upvotes: 0

Views: 205

Answers (2)

Samjack
Samjack

Reputation: 188

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.

Upvotes: 0

Rajkumar Nagarajan
Rajkumar Nagarajan

Reputation: 1091

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

Upvotes: 1

Related Questions