How to automatically determine item fit count for RecyclerView grid

1958 views java

Is it possible to use a specified width (100dp) to equally space out items in a Grid (as many as possible) for a RecyclerView rather than using an exact number of columns? int numberOfColumns = 3;, int numberOfColumns = 4;, etc. will not do the job as this will lead to plenty of empty space being present on larger screens.

portrait orientation

enter image description here

landscape orientation

enter image description here

RecyclerView-related code in Fragment

                static final String[] frenchVowels = new String[]{
                    "a", "e", "i", "o", "u", "y"

                RecyclerViewAdapter rvAdapterGL;
                final RecyclerView rvGL = viewHolder.itemView.findViewById(;
                int numberOfColumns = 2;
                rvGL.setLayoutManager(new GridLayoutManager(getActivity(), numberOfColumns));
                rvAdapterGL = new RecyclerViewAdapter(getActivity(), frenchVowels);

RecyclerView adapter class

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private String[] mData;
    private LayoutInflater mInflater;

    // data is passed into the constructor
    public RecyclerViewAdapter(Context context, String[] data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;

    // inflates the cell layout from xml when needed
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.gridview_item, parent, false);
        return new RecyclerViewAdapter.ViewHolder(view);

    // binds the data to the TextView in each cell
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {

    // total number of cells
    public int getItemCount() {
        return mData.length;

    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView myTextView;

        ViewHolder(View itemView) {
            myTextView = itemView.findViewById(;

answered question

2 Answers


There are a few different ways to handle this. One thing you could do is get the screen width and divide it by your desired cell width to get the number of columns.

You could also use the Resources framework to set different column counts for different screen sizes. For example, you could have a res/values/integers.xml that sets the column count to 3, and also a res/values-sw400dp/integers.xml that sets the column count to 5, and so on.


    <integer name=“column_count”>3</integer>


    <integer name=“column_count”>5</integer>

Now you can replace

int numberOfColumns = 2;


int numberOfColumns = getResources().getInteger(R.integer.column_count);

And you can use any number of values-swXXXdp/integers.xml files with lots of different values for XXX to have lots of different column counts for lots of different screen sizes.

posted this

If you use GridView layout, this question is answered here.

Or, if you want to use GridLayoutManager, do it like this

if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
     mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
     mRecycler.setLayoutManager(new GridLayoutManager(mContext, 4));

posted this

Have an answer?


Please login first before posting an answer.