title: Add Vertical and Horizontal Scrolling Programmatically to LinearLayout in Android
body: <p>I'm trying to create a layout of a activity as follows:
  <img src="https://i.stack.imgur.com/5xzhM.png" alt="enter image description here"></p>
  
  <p>the <code>game_board.xml</code> code is is follows:</p>
  
  <pre><code>&lt;LinearLayout 
          android:id="@+id/layoutFilterContent"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:weightSum="4"
          android:background="#837E7C" &gt;
  
          &lt;LinearLayout 
              android:id="@+id/layoutTopicGrade"
              android:orientation="vertical"
              android:layout_width="0dp"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:padding="0dp"
              android:background="#BCC6CC" &gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout 
              android:id="@+id/layoutGames"
              android:orientation="vertical"
              android:layout_width="0dp"
              android:layout_height="fill_parent"
              android:layout_weight="3"
              android:padding="0dp"
              android:background="#E5E4E2" &gt;
  
          &lt;/LinearLayout&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The <code>java</code> code in <code>GameBoardActivity.java</code> is as follows:</p>
  
  <pre><code>import android.os.Bundle;
  import android.annotation.SuppressLint;
  import android.app.ActionBar.LayoutParams;
  import android.app.Activity;
  import android.view.Menu;
  import android.widget.ImageButton;
  import android.widget.LinearLayout;
  import android.widget.ScrollView;
  
  public class GameBoardActivity extends Activity {
  
      LinearLayout topicGrade;
      LinearLayout gameContent;
      ScrollView scroll;
      ImageButton[] imgBtn;
  
      @SuppressWarnings("deprecation")
      @SuppressLint({ "NewApi", "InlinedApi", "ResourceAsColor" })
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.game_board);
  
          topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
  
          scroll = new ScrollView(this);
          scroll.setBackgroundColor(android.R.color.transparent);
          scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
          scroll.addView(topicGrade);
  
          imgBtn = new ImageButton[15];
  
          for(int i=0;i&lt;15;i++){
              imgBtn[i] = new ImageButton(this);
              imgBtn[i].setId(i);
              imgBtn[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
              topicGrade.addView(imgBtn[i]);
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.game_board, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>I'm getting <code>error</code> as: <code>The specified child already has a parent. You must call removeView() on the child's parent first.</code><br><br>
  I first tried adding buttons to <code>LinearLayout1</code> without <code>ScrollView</code>, and I'm able to add the buttons as I want, but when I implement <code>ScrollView</code> I'm getting the error as mentioned above. So I got stuck and didn't try anything on <code>LinearLayout2</code><br> <br>
  Please anyone help me to design the activity shown above, </p>