title: How to add a copy of XML dynamically
body: <p>I am making an Android app, and I want to copy some XML code in a Linear Layout, and re-insert it into the Linear Layout so that there are two of the Relative Layouts in the Linear Layout. I would like to do this dynamically by taking this code below:</p>
  
  <pre><code> &lt;LinearLayout
          android:id="@+id/tileContainerME"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" &gt;
  
          &lt;RelativeLayout
          android:id="@+id/tilesAreHERE"
          android:layout_width="207dp"
          android:layout_height="151dp" &gt;
  
          &lt;TextView
              android:id="@+id/bottom1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/top1"
              android:layout_alignParentBottom="true"
              android:gravity="center_horizontal"
              android:text="1"
              /&gt;
  
          &lt;TextView
              android:id="@+id/left1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"
              android:gravity="center_vertical"
              android:text="2"
               /&gt;
  
          &lt;TextView
              android:id="@+id/right1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/left1"
              android:layout_alignBottom="@+id/left1"
              android:layout_alignParentRight="true"
              android:gravity="center_vertical"
              android:text="3"
             /&gt;
  
          &lt;TextView
              android:id="@+id/top1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:gravity="center_horizontal"
              android:text="4"
               /&gt;
  
      &lt;/RelativeLayout&gt;            
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And then simply turning it into this:</p>
  
  <pre><code> &lt;LinearLayout
          android:id="@+id/tileContainerME"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" &gt;
          &lt;RelativeLayout
          android:id="@+id/tilesAreHERE"
          android:layout_width="207dp"
          android:layout_height="151dp" &gt;
  
          &lt;TextView
              android:id="@+id/bottom1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/top1"
              android:layout_alignParentBottom="true"
              android:gravity="center_horizontal"
              android:text="1"
              /&gt;
  
          &lt;TextView
              android:id="@+id/left1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"
              android:gravity="center_vertical"
              android:text="2"
               /&gt;
  
          &lt;TextView
              android:id="@+id/right1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/left1"
              android:layout_alignBottom="@+id/left1"
              android:layout_alignParentRight="true"
              android:gravity="center_vertical"
              android:text="3"
             /&gt;
  
          &lt;TextView
              android:id="@+id/top1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:gravity="center_horizontal"
              android:text="4"
               /&gt;
  
      &lt;/RelativeLayout&gt;  
  
          &lt;RelativeLayout
          android:id="@+id/tilesAreHERE"
          android:layout_width="207dp"
          android:layout_height="151dp" &gt;
  
          &lt;TextView
              android:id="@+id/bottom1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/top1"
              android:layout_alignParentBottom="true"
              android:gravity="center_horizontal"
              android:text="1"
              /&gt;
  
          &lt;TextView
              android:id="@+id/left1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"
              android:gravity="center_vertical"
              android:text="2"
               /&gt;
  
          &lt;TextView
              android:id="@+id/right1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/left1"
              android:layout_alignBottom="@+id/left1"
              android:layout_alignParentRight="true"
              android:gravity="center_vertical"
              android:text="3"
             /&gt;
  
          &lt;TextView
              android:id="@+id/top1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:gravity="center_horizontal"
              android:text="4"
               /&gt;
  
      &lt;/RelativeLayout&gt;                 
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>See how there would be TWO RelativeLayout sections... I would like to basically make a copy of one and then add it back in (I don't really know how many times I might have to do this in my program, that's why I am not literally inserting it into the XML, I would like to do it from the Java code).
  This is what I have so far, but whenever I run it, the layout is wrong. What might be wrong with my code?</p>
  
  <pre><code>            LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
      RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);     
      RelativeLayout m2 = new RelativeLayout(this);
  
      m2.setLayoutParams(m.getLayoutParams());
      m2.setGravity(m.getGravity());
      m2.setLayoutDirection(m.getLayoutDirection());
  
  
      TextView et1 = (TextView) findViewById(R.id.bottom1);
      TextView et2 = (TextView) findViewById(R.id.left1);
      TextView et3 = (TextView) findViewById(R.id.right1);
      TextView et4 = (TextView) findViewById(R.id.top1);
  
      TextView tv1 = new TextView(et1.getContext());
      TextView tv2 = new TextView(et2.getContext());
      TextView tv3 = new TextView(et3.getContext());
      TextView tv4 = new TextView(et4.getContext());
  
      tv1.setLayoutDirection(et1.getLayoutDirection());
      tv2.setLayoutDirection(et2.getLayoutDirection());
      tv3.setLayoutDirection(et3.getLayoutDirection());
      tv4.setLayoutDirection(et4.getLayoutDirection());
  
      tv1.setGravity(et1.getGravity());
      tv2.setGravity(et2.getGravity());
      tv3.setGravity(et3.getGravity());
      tv4.setGravity(et4.getGravity());
  
      tv1.setText(et1.getText());
      tv2.setText(et2.getText());
      tv3.setText(et3.getText());
      tv4.setText(et4.getText());
  
      m2.addView(tv4,et4.getLayoutParams());
      m2.addView(tv3,et3.getLayoutParams());
      m2.addView(tv2,et2.getLayoutParams());
      m2.addView(tv1,et1.getLayoutParams());  
  
      m3.addView(m2);
  </code></pre>
  
  <p>I don't see what's wrong with my code, any suggestions.....</p>
  
  <p>THIS IS THE FULL XML DATA FILE:</p>
  
  <pre><code>         &lt;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" &gt;
    &lt;HorizontalScrollView
          android:id="@+id/scrollHORIZON"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:layout_toRightOf="@+id/linearLayout1" &gt;
  
  
  
          &lt;LinearLayout
              android:id="@+id/tileContainerME"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal" &gt;
  
              &lt;RelativeLayout
              android:id="@+id/tilesAreHERE"
              android:layout_width="207dp"
              android:layout_height="151dp" &gt;
  
              &lt;TextView
                  android:id="@+id/bottom1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignLeft="@+id/top1"
                  android:layout_alignParentBottom="true"
                  android:gravity="center_horizontal"
                  android:text="1"
                  /&gt;
  
              &lt;TextView
                  android:id="@+id/left1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_centerVertical="true"
                  android:gravity="center_vertical"
                  android:text="2"
                   /&gt;
  
              &lt;TextView
                  android:id="@+id/right1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignBaseline="@+id/left1"
                  android:layout_alignBottom="@+id/left1"
                  android:layout_alignParentRight="true"
                  android:gravity="center_vertical"
                  android:text="3"
                 /&gt;
  
              &lt;TextView
                  android:id="@+id/top1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:layout_centerHorizontal="true"
                  android:gravity="center_horizontal"
                  android:text="4"
                   /&gt;
  
          &lt;/RelativeLayout&gt;           
          &lt;/LinearLayout&gt;
  
  
      &lt;/HorizontalScrollView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>