title: Creating LinearLayout Programmatically/Dynamically with Multiple Views
body: <p>I have a hierarchy that is like this:</p>
  
  <ul>
  <li>LinearLayout(horizontal)
  <ul>
  <li>ImageView</li>
  <li>LinearLayout(vertical)
  <ul>
  <li>TextView</li>
  <li>TextView</li>
  <li>TextView</li>
  <li>TextView</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I want to be able to add the hierarchy above through iteration as long as there is data that could be obtained from the database(using <a href="http://parse.com">Parse</a>)</p>
  
  <p>I have tried putting up the ImageView and LinearLayout under the parent LinearLayout but it doesn't seem to work. Here is my code in MainActivity.Java:</p>
  
  <pre><code>LinearLayout LL_Outer = (LinearLayout) findViewById(R.id.new_linearLayoutOuter);
  LL_Outer.setOrientation(LinearLayout.VERTICAL); // set orientation
  LL_Outer.setBackgroundColor(color.white); // set background
  // set Layout_Width and Layout_Height
  LinearLayout.LayoutParams layoutForOuter = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
  LL_Outer.setLayoutParams(layoutForOuter);
  
  
  LinearLayout LL_Inner = (LinearLayout) findViewById(R.id.new_linearLayoutInner);
  LL_Inner.setOrientation(LinearLayout.HORIZONTAL);
  LL_Inner.setBackgroundColor(color.white);
  LinearLayout.LayoutParams layoutForInner = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  LL_Inner.setLayoutParams(layoutForInner);
  
  //LL_Outer.addView(LL_Inner);
  
  ImageView IV = (ImageView) findViewById(R.id.new_imageViewPP);
  //IV.getLayoutParams().height = 55;
  //IV.getLayoutParams().width = 55;
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  params.setMargins(14, 12, 0, 0);
  params.height = 55;
  params.weight = 55;
  IV.setBackgroundColor(color.black);
  IV.setLayoutParams(params);
  
  LL_Inner.addView(IV);
  LL_Outer.addView(LL_Inner);
  </code></pre>
  
  <p>I don't know where I went wrong as my code did not prompt any error. Please help.</p>
  
  <p>EDIT: I have edited the Orientations accordingly and when I run the app, it stops working. And prompts an error in LogCat saying "The specified child already has a parent. You must call removeView() on the child's parent first.</p>
  
  <p>Here's my XML:</p>
  
  <pre><code>&lt;LinearLayout
      android:id="@+id/new_linearLayoutOuter"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@color/white"
      android:orientation="horizontal" &gt;
  
      &lt;ImageView
              android:id="@+id/new_imageViewPP"
              android:layout_width="55dp"
              android:layout_height="55dp"
              android:layout_marginLeft="14dp"
              android:layout_marginTop="12dp"
              android:background="@color/black"
              android:contentDescription="@string/pp" /&gt;
  
      &lt;LinearLayout
          android:id="@+id/new_linearLayoutInner"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@color/white"
          android:orientation="vertical" &gt;
  
          &lt;TextView 
              android:id="@+id/new_textView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/movie_title"
              android:paddingTop="10dp"
              android:paddingLeft="7dp"
              android:textSize="15sp" /&gt; &lt;!-- Title of the movie --&gt;
  
          &lt;TextView 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/review_by"
              android:paddingTop="3dp"
              android:paddingLeft="7dp"
              android:textSize="12sp" /&gt; &lt;!-- By --&gt;
  
          &lt;TextView 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/movie_stars"
              android:paddingTop="3dp"
              android:paddingLeft="7dp"
              android:textSize="12sp" /&gt; &lt;!-- Rating and date --&gt;
  
          &lt;TextView 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/sample_string"
              android:maxLines="5"
              android:scrollbars="vertical"
              android:paddingTop="10dp"
              android:paddingLeft="7dp"
              android:textSize="12sp"
              android:layout_gravity="center_vertical|right" /&gt; &lt;!-- Review content --&gt;
  
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>