I'm trying to center all my items horizontally for each row in RecyclerView
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
item_row.xml
but I didn't succeed :(
What i have
What i want
In activity
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
item_row.xml
<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">
<!-- Recycler View Item Row -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="@android:color/black" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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" />
<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"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
i googled a lot and found this answer but actually i don't know how to create a LinearLayout
for each row of items!
is that the best solution?
any other idea?