How to create 3 generic card views?

advertisements

I'm trying to create 3 cardviews, each have the same height, weight etc..

I managed to create one:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="8dp"
    android:padding="8dp">

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:scaleType="centerInside"
        android:src="@drawable/moon20"
        android:background="@android:color/white"
        android:padding="8dp"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:padding="8dp"
        android:text="20 min Power Nap"
        android:textColor="@color/colorSecondaryText"
        android:textStyle="bold"
        android:textSize="20dp"
        android:textAlignment="center"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />

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

I need two more of this cardview, but I'm getting

"multiple root tag" error

Do I have to create a base layout like Relative layout for all cardviews?


You need a root view to put inside your CardViews. Right now you are putting one CardView inside the other (Because your root is a CardView). Try to put them inside a LinearLayout.

<LinearLayout>
     <CardView>
     <CardView>
     <CardView>
</LinearLayout>