Create RelativeView inside CardView programmatically android studio

1.6k views Asked by At

I am trying to create a history activity page for my app. I get data from an sql table with the users data, and set that to a card. I have created the first card in my History activity, and I do not want to pre-create all the cards, but I do not know how to programmatically add more card views. I have a relative layout inside a card view, so I can organize the data how I want. Here is my xml layout for the activity

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"    
     android:layout_width="match_parent"
     xmlns:ads="http://schemas.android.com/apk/res-auto"
     xmlns:card_view="http://schemas.android.com/apk/res-auto"
     android:layout_height="match_parent"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     android:paddingBottom="@dimen/activity_vertical_margin">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear History"
        android:id="@+id/clearSQLite"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:width="150dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tips"
        android:id="@+id/TipsButton"
        android:width="150dp"
        android:layout_above="@+id/adView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <android.support.v7.widget.CardView
        tools:context="com.winansbros.soccerpredictor.History"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:id="@+id/team1imageview"
                android:layout_alignParentTop="true"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/team1name"
                android:layout_centerVertical="true"
                android:layout_below="@+id/team2name"
                android:layout_toRightOf="@+id/team1imageview"
                android:layout_toEndOf="@+id/team1imageview"
                android:width="100dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/card1score"
                android:width="20dp"
                android:layout_centerVertical="true"
                android:layout_alignTop="@+id/team1name"
                android:layout_toRightOf="@+id/team1name"
                android:layout_toEndOf="@+id/team1name"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/team2name"
                android:width="100dp"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/team2imageview"
                android:layout_toStartOf="@+id/team2imageview"/>
            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:id="@+id/team2imageview"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"/>

        </RelativeLayout>

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

 </RelativeLayout>

How do I create another version of the card view layout I have in java? Thank you in advance

1

There are 1 answers

0
cameronlund4 On BEST ANSWER

Check out RecyclerView. If you set it up right, it uses the card as a template and will create cards based off of an array of data (which is customizeable.) This tutorial should help.