Home » Android » How to create GridView image gallery programmatically in android?

How to create GridView image gallery programmatically in android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to create 3×3 gridview programmatically for displaying images. And I want to set each item height and width by getting screen dimensions. Like:

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

Then each item width = screenWidth/3;

Please someone help me or some link for reference.

How to&Answers:

This is how you can create gridview programmatically,

    GridView grid = new GridView(this);
    grid.setId(ViewIdentification.getId());
    grid.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    grid.setBackgroundColor(Color.WHITE);
    grid.setNumColumns(3);
    grid.setColumnWidth(GridView.AUTO_FIT);
    grid.setVerticalSpacing(5);
    grid.setHorizontalSpacing(5);
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

Add the above view to your layout. And here you can get the height and width of the display.

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
            .getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;

And here is the adapter class :

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private Bitmap[]mis_fotos;

    public ImageAdapter(Context c) {
        mContext = c;    }

    public int getCount() {
        return mis_fotos.length;
        }

    public Object getItem(int position) {
        return position;    }

    public long getItemId(int position) {
        return 0;    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(width/3, height/3));
            imageView.setScaleType(ImageView.setScaleType(ScaleType.FIT_XY));
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(mis_fotos[position]);
        return imageView;
    }
}

Then, It’s upto you, If you are passing dynamic URL, change your adapter accordingly.
set your adapter to your gridview.
Let me know if you have any issues.

Answer:

In your Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridView gridView = new GridView(this);
gridView.setNumColumns(3);
setContentView(gridView);
gridView.setAdapter(new GridViewAdapter(this));
}

And Adapter:

private class GridViewAdapter extends BaseAdapter {
private Context context;
private final int length = 9;
public GridViewAdapter(Context context) {
            this.context = context;
}
@Override
        public int getCount() {
            return length;
        }
@Override
        public Object getItem(int position) {
            return null;
        }
@Override
        public long getItemId(int position) {
            return 0;
        }
@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                DisplayMetrics metrics = context.getResources().getDisplayMetrics();
                int width = metrics.widthPixels / 3;
                int height = metrics.heightPixels / 3;
                imageView.setLayoutParams(new GridView.LayoutParams(width, height));
            } else {
                imageView = (ImageView) convertView;
            }
imageView.setImageResource(R.drawable.ic_launcher);
            return imageView;
        }
    }