0

I want to have a cardview on a recycler view.The problem is that the cards are not aligned correctly and they have a big space between them(see image).I don't know if it's because i've done them with fragments(first time i'm using them because i'm trying to learn them)

Here are my xml's:

fragment_food.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_food"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        />

</RelativeLayout>

card_layout_food.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:tag="cards main container">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="@color/colorAccent"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="5dp"
    card_view:cardUseCompatPadding="true">



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textViewName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="Android Name"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textViewVersion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"

                android:text="Android Version"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>

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

Alex
  • 1,816
  • 5
  • 23
  • 39

1 Answers1

1

In card_layout_food.xml set

android:layout_height="wrap_content"

to the root layout so every item in the recyclerview has only as much height as needed.
For the cardview:

android:layout_margin="0dp"
card_view:cardElevation="3dp"
forpas
  • 160,666
  • 10
  • 38
  • 76
  • Thanks for that!Now it looks like this [link](https://ibb.co/Kwm99MM) .Any idea on how to make them more close to each other? – Alex Dec 19 '18 at 22:39
  • If you don't want the margin attribute, set it to 1dp or 0dp, but are you sure you want them so close? – forpas Dec 19 '18 at 22:41
  • Yeah i want to be close because it doesn't look nice like this.But the margin you told me didn't work :/ – Alex Dec 19 '18 at 22:44
  • The space between the cards is affected by margin. If it is 0dp, maybe you can set less elevation: `card_view:cardElevation="3dp"` – forpas Dec 19 '18 at 22:46