center items horizontally in RecyclerView(using GridLayoutManager)
<p>I'm trying to center all my items horizontally for each row in <code>RecyclerView</code></p>

<p>I tried many things: RelativeLayout as a parent, then use layout_centerInParent, use the basic android:layout_gravity="center", try to add some space element,using weights and .... in <code>
item_row.xml</code><br>
but I didn't succeed :(</p>

<p><strong>What i have</strong></p>

<p><img src="https://i.stack.imgur.com/NXQI0.png" alt="a busy cat"></p>

<p><strong>What i want</strong></p>

<p><img src="https://i.stack.imgur.com/nqWUd.png" alt="a busy cat"></p>

<p><strong>In activity</strong></p>

<pre><code>recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
</code></pre>

<p><strong>item_row.xml</strong></p>

<pre><code>&lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"&gt;

&lt;!--   Recycler View Item Row   --&gt;
&lt;LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"&gt;


    &lt;ImageView
        android:id="@+id/imgThumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black" /&gt;

    &lt;LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"&gt;


        &lt;TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="item name"
            android:id="@+id/txtName"
            android:layout_margin="5dp"
            android:singleLine="false"
            android:lines="2"
            android:gravity="center" /&gt;

        &lt;LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"&gt;

            &lt;TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/txtCategory"
                android:textStyle="italic"
                android:textColor="#496fa9"
                android:text="subtitle"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:layout_gravity="center" /&gt;

            &lt;ImageButton
                android:id="@+id/imageButton"
                android:layout_width="48dp"
                android:layout_height="wrap_content"
                android:background="@android:color/black"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:padding="8dp"/&gt;

        &lt;/LinearLayout&gt;

    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;

&lt;/android.support.v7.widget.CardView&gt;
</code></pre>

<p>i googled a lot and found this <a href="https://stackoverflow.com/questions/30007956/how-to-center-items-of-a-recyclerview">answer</a> but actually i don't know how to create a <code>LinearLayout</code> for each row of items!</p>

<ul>
<li><p>is that the best solution?</p></li>
<li><p>any other idea?</p></li>
</ul>