Centering CardView in RecyclerView?

I play with CardViews inside RecyclerView, but I can't seem to have my cards centered.

My cardview.xml:

<android.support.v7.widget.CardView
    android:id="@+id/cardview"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:layout_width="300dp"
    android:layout_height="100dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/text"
        android:textAlignment="center"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</android.support.v7.widget.CardView>

My activity_main.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>

    <!-- A RecyclerView with some commonly used attributes -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

An image of what my application looks like at startup:

+4
source share
5 answers

Okok, I have the same question, and I think I "solved" it. This is just another hack. The android team at Google really needs to fix this recycler scan and add support for deletion to reject.

In onCreateView:

recyclerView.addItemDecoration(new ItemDecoration() {

  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
    int totalWidth = parent.getWidth();
    int maxCardWidth = getResources().getDimensionPixelOffset(R.dimen.card_max_width);
    int sidePadding = (totalWidth - maxCardWidth) / 2;
    sidePadding = Math.max(0, sidePadding);
    outRect.set(sidePadding, 0, sidePadding, padding);
  }
});
+6
source

You need to set LinearLayout android:layout_widthhow match_parentto android:gravity="center"take effect.

+2
source

. RecyclerView SwipeRefreshLayout, , SwipeRefreshLayout.

<style name="swipe_refresh_cards_layout">
    <item name="android:layout_width">@dimen/swipe_refresh_cards_width</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_gravity">center_horizontal</item>
</style>

dimens.xml .

Not the best IMO solution, but it works. I also found that it works (not using the solution above) if I put LinearLayout as a parent in SwipeRefreshLayout with

<style name="inner_layout">
    <item name="android:layout_width">@dimen/swipe_refresh_cards_width</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_gravity">center_horizontal</item>
    <item name="android:orientation">vertical</item>
</style>
+2
source

Try wrapping your Cardview inside LinearLayout with the width set for match_parent and android: gravity = "center".

0
source

Just set the width of the parent (in your case LinearLayout) to match_parent.

0
source

All Articles