title: Equal height and width of RecyclerView items
body: <p>As you can see from the images below, I have almost achieved what I wanted. However, the <code>RecyclerView</code> items don't have equal size. In particular, the 2nd <code>RecyclerView</code> column is different in size from the 1st column.</p>
  
  <p>Note that I have tried implementing the solution from <a href="https://stackoverflow.com/questions/32063565/how-to-make-recyclerviews-grid-item-to-have-equal-height-and-width">How to make RecyclerView's grid item to have equal height and width?</a>, but I couldn't resolve my issue.</p>
  
  <p><a href="https://i.stack.imgur.com/c4Y2a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c4Y2a.png" alt="Result of my code"></a></p>
  
  <p>RecyclerView item:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;com.jackandphantom.circularimageview.RoundedImage
          android:id="@+id/launcher_icon"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:scaleType="centerCrop"
          app:layout_constraintDimensionRatio="1:1"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>XML fragment:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/white"
      android:orientation="vertical"&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id="@+id/recycler"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginLeft="@dimen/medium_margin"
          android:layout_marginRight="@dimen/medium_margin" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Fragment.java:</p>
  
  <pre><code> public View onCreateView(@NonNull LayoutInflater inflater,
                               ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_home, container, false);
  
          RecyclerView recyclerView = view.findViewById(R.id.recycler);
          recyclerView.setHasFixedSize(true);
          recyclerView.addItemDecoration(new ItemOffsetDecoration(getActivity(), R.dimen.medium_margin));
          GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
          recyclerView.setLayoutManager(layoutManager);
  
          RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity());
          recyclerView.setAdapter(adapter);
  
          return view;
      }
  </code></pre>
  
  <p>ItemOffsetDecoration.java</p>
  
  <pre><code>public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
      private int mItemOffset;
  
      private ItemOffsetDecoration(int itemOffset) {
          mItemOffset = itemOffset;
      }
  
      public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) {
          this(context.getResources().getDimensionPixelSize(itemOffsetId));
      }
  
      @Override
      public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
                                 @NonNull RecyclerView.State state) {
          super.getItemOffsets(outRect, view, parent, state);
  
          int position = parent.getChildLayoutPosition(view);
  
          GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();
  
          if (manager != null &amp;&amp; position &lt; manager.getSpanCount()) outRect.top = mItemOffset;
  
          if (position % 2 != 0) {
              outRect.right = mItemOffset;
          }
  
          outRect.left = mItemOffset;
          outRect.bottom = mItemOffset;
      }
  }
  </code></pre>