How to create a 3/2 grid using grid view with base adapter android in java

96 Views Asked by At

6 grid should be shown without scrolling, that is it should be like setting layout_weight="1" & layout_height="1" for every grid cards.

6 cards should be shown respective of screen size.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/gvImages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        app:layout_constrainedHeight="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintVertical_weight="1"
        app:layout_constraintHorizontal_weight="1"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"
        tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>

MyAdapter.java

package com.aijishnu.baseadaptersample;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.aijishnu.baseadaptersample.databinding.GridLayoutBinding;

public class MyAdapter extends BaseAdapter {

    Context c;
    int items[];

    MyAdapter(Context c, int arr[]) {
        this.c = c;
        items = arr;
    }
    @Override
    public int getCount() {
        return items.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) {

        ViewHolder holder;
        if (convertView == null) {

            GridLayoutBinding itemBinding = GridLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent,false);

            holder = new ViewHolder(itemBinding);
            holder.view = itemBinding.getRoot();
            holder.view.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        holder.binding.imageView.setImageResource(items[position]);
        return holder.view;
    }

    private static class ViewHolder {
        private View view;
        private GridLayoutBinding binding;

        ViewHolder(GridLayoutBinding binding) {
            this.view = binding.getRoot();
            this.binding = binding;
        }
    }
}

MainActivity.java

package com.aijishnu.baseadaptersample;

import android.os.Bundle;

import com.aijishnu.baseadaptersample.databinding.ActivityMainBinding;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        int[] itemsarray = new int[] {
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
        };

        MyAdapter adapter = new MyAdapter(this, itemsarray);
        binding.gvImages.setAdapter(adapter);
    }
}

Above code results as like this, (This is not fit to screen height, also have scrolling option, I want 6 cards should fit to screen size) enter image description here

I want to have like this(all cards equals according to screen size) enter image description here

NB: These cards should be randomly generated using BaseAdapter or RecyclerAdapter

1

There are 1 best solutions below

1
Jay Garzon On

You can set the number of rows and columns of your GridLayout inside your xml file