title: How do you create a custom CardView object from an XML layout file?
body: <p>I'm trying to create a custom CardView object called <em>WeekView</em>. <em>WeekView</em> extends CardView. I have a CardView layout in an XML file. How do I make the <em>WeekView</em> "copy" the layout from the XML file?</p>
  
  <p>Simply put, I want to be able to do this:</p>
  
  <pre><code>WeekView weekCard = new WeekView();
  mainLayout.addView(weekCard);
  </code></pre>
  
  <p>in the main activity, where <em>mainLayout</em> is an empty linear layout. The WeekCard should look like the card in the XML layout file.</p>
  
  <p>Here's a shortened version of the XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;android.support.v7.widget.CardView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:tipbook="http://schemas.android.com/apk/res-auto"
  android:id="@+id/week_card_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginLeft="8dp"
  android:layout_marginRight="8dp"
  android:layout_marginTop="8dp"
  tipbook:contentPadding="12dp"&gt;
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;TextView
          android:text="Week View"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/week_view_title"
          android:textSize="12sp"
          android:textAlignment="gravity"
          android:gravity="center"
          android:layout_alignParentTop="true"
          android:layout_alignParentLeft="true"
          android:textColor="@android:color/darker_gray"
          android:layout_marginTop="4dp"
          android:layout_marginLeft="4dp" /&gt;
  
      &lt;ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          tipbook:srcCompat="@drawable/ic_more_vert_black_18dp"
          android:id="@+id/week_settings_btn"
          android:background="@color/cardview_light_background"
          android:scaleX="1"
          android:scaleY="1"
          android:layout_alignParentTop="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          android:tint="@color/cardview_shadow_start_color"
          android:tintMode="src_in"
          android:layout_marginRight="4dp"
          android:layout_marginTop="4dp" /&gt;
  
  
      &lt;LinearLayout
          android:id="@+id/week_days"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="55dp"
          android:layout_below="@id/week_settings_btn"
          android:weightSum="7"
          android:layout_marginTop="12dp" &gt;
  
  
  
      &lt;/LinearLayout&gt;
  
      &lt;TextView
          android:text="Last Week"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/week_days"
          android:id="@+id/visible_week"
          android:textSize="14sp"
          android:textAlignment="gravity"
          android:gravity="center"
          android:layout_marginTop="12dp" /&gt;
  
      &lt;ImageButton
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/week_days"
          android:layout_marginTop="12dp"
          tipbook:srcCompat="@drawable/ic_expand_more_black_18dp"
          android:id="@+id/more_less_week"
          android:scaleX="1"
          android:scaleY="1"
          android:tint="@color/common_google_signin_btn_text_light_default"
          android:tintMode="src_in"
          android:background="@color/cardview_light_background"
          android:visibility="gone"/&gt;
  &lt;/RelativeLayout&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>