title: How can I dynamically add layouts and views to an Android XML layout file?
body: <p>I am creating an app that needs to dynamically generate an XML file. The layout starts essentially empty (other than a linear layout), then I loop through a JSON array and create the XML code shown below for EACH element in the array (the only thing different for each element in the array is the ids for the views). </p>
  
  <p>I am not really understanding how I can use Java to create these layouts, edit  their attributes, and add views to them.</p>
  
  <p>I know I can create a GridLayout object and give it the number of rows and columns with</p>
  
  <pre><code>GridLayout doc = new GridLayout(3, 3);
  </code></pre>
  
  <p>But I can't figure out how to edit all of the specific attributes, and then add views  inside of the layout. </p>
  
  <p>What is the best way to create a layout, edit it's attributes, and add views within that layout through Jave code?</p>
  
  <p>Thank you.</p>
  
  <pre><code>&lt;GridLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:columnCount="3"
      android:rowCount="3"
      android:layout_marginBottom="10dp"
      android:background="#3c37ff00"
      android:id="@+id/doctor1"
      android:longClickable="true"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:text="Dr. Sam"
          android:id="@+id/doctor1_name"
          android:layout_row="0"
          android:layout_column="0"
          android:textStyle="bold"
          android:textSize="26dp"
          android:typeface="sans" /&gt;
  
      &lt;ImageButton
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:id="@+id/doctor1_profile"
          android:layout_row="0"
          android:layout_column="1"
          android:src="@drawable/no_pic"
          android:scaleType="fitXY"
          android:layout_rowSpan="3"
          android:layout_columnSpan="1"
          android:layout_gravity="right"
          android:background="#00ffffff" /&gt;
  
      &lt;ImageButton
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:id="@+id/doctor1_action"
          android:layout_row="0"
          android:layout_column="2"
          android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"
          android:scaleType="centerInside"
          android:layout_rowSpan="3"
          android:layout_columnSpan="1"
          android:background="#47ffffff"
          android:clickable="true"
          android:onClick="setContentView" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="Very Close"
          android:id="@+id/doctor1_distance"
          android:layout_row="1"
          android:layout_column="0"
          android:textStyle="bold" /&gt;
  
  &lt;/GridLayout&gt;
  </code></pre>