title: Error "The specified child already has a parent" after LinearLayout.addView() and then Linearlayout.removeView()
body: <p>I'm working on a project where I have a parent LinearLayout with id=<em>linear_parent</em> created on activity_main.xml. Inside this parent layout, n numbers of LinearLayouts are created <strong>programmatically</strong> base on user input at EditText (id=<em>no_of_views_input</em>).
  And inside the each of these Linearlayouts, there are more child views (<em>imageView, edit, button</em>) created programmatically.</p>
  <p>Suppose a user enters 4 <em>no_of_views_input</em>, the views heirarchy would be something like,</p>
  <p>============</p>
  <ol>
  <li><em><strong>linear_parent</strong></em>
  <ul>
  <li><em><strong>linearLayout</strong></em>   //position 1
  <ul>
  <li><em>imageView</em></li>
  <li><em>edit</em></li>
  <li><em>button</em></li>
  </ul>
  </li>
  <li><em><strong>linearLayout</strong></em>   //position 2
  <ul>
  <li><em>imageView</em></li>
  <li><em>edit</em></li>
  <li><em>button</em></li>
  </ul>
  </li>
  <li><em><strong>linearLayout</strong></em>   //position 3
  <ul>
  <li><em>imageView</em></li>
  <li><em>edit</em></li>
  <li><em>button</em></li>
  </ul>
  </li>
  <li><em><strong>linearLayout</strong></em>   //position 4
  <ul>
  <li><em>imageView</em></li>
  <li><em>edit</em></li>
  <li><em>button</em></li>
  </ul>
  </li>
  </ul>
  </li>
  </ol>
  <p>===========</p>
  <p>After these layouts and views are created, I want to rearrange the LinearLayouts, base on user input from child id=<em>edit</em>.
  Suppose the user enters 1 on <em>edit</em> at LinearLayout position 3 , what I want is, the current LinearLayout at position 3 must be added to position 1 along with its children, and finally remove the old Linearlayout which was at position 3. (would be currently at position 4 if indexes are shifted down).</p>
  <p><em>activity_main.xml</em></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:gravity=&quot;center&quot;
      android:orientation=&quot;vertical&quot;&gt;
      
      &lt;LinearLayout
          android:id=&quot;@+id/linear_parent&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginTop=&quot;20dp&quot;
          android:gravity=&quot;center&quot;
          android:orientation=&quot;vertical&quot;&gt;
  
                 &lt;!-- Views are added here programmatically --&gt;
  
      &lt;/LinearLayout&gt;
      
      &lt;EditText
          android:id=&quot;@+id/no_of_views_input&quot;
          android:layout_width=&quot;100dp&quot;
          android:layout_height=&quot;40dp&quot;
          android:hint=&quot;Number of view to add&quot;/&gt;
      &lt;Button
          android:id=&quot;@+id/add_button&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Add&quot;/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p><em>MainActivity.java</em></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      LinearLayout linear_parent;
      Button add_button;
      EditText no_of_views_input;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          linear_parent = findViewById(R.id.linear_parent);
          add_button= findViewById(R.id.add_button);
          no_of_views_input= findViewById(R.id.no_of_views_input);
  
          no_of_views_input.setInputType(InputType.TYPE_CLASS_NUMBER);
  
          add_button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (!no_of_views_input.getText().toString().isEmpty()) {
  
                      String no_of_views_temp = no_of_views_input.getText().toString();
                      int no_of_views = Integer.parseInt(no_of_views_temp);
  
                      for (int i = 0; i &lt; no_of_views; i++) {
                          LinearLayout linearLayout = new LinearLayout(MainActivity.this);
                          LinearLayout.LayoutParams lp_ll = new LinearLayout.LayoutParams(600, 1500);
                          lp_ll.setMargins(0, 50, 0, 0);
                          linearLayout.setOrientation(LinearLayout.VERTICAL);
                          linearLayout.setId(View.generateViewId()); // id = 1, 2, 3, 4, ...
                          int linearLayoutID = linearLayout.getId();
                          linearLayout.setLayoutParams(lp_ll);
  
                          ImageView imageView = new ImageView(MainActivity.this);
                          LinearLayout.LayoutParams lp_image = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 800);
                          imageView.setBackgroundColor(Color.Yellow);
                          imageView.setLayoutParams(lp_image);
  
                          EditText edit = new EditText(MainActivity.this);
                          LinearLayout.LayoutParams lp_edit = new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.WRAP_CONTENT);
                          edit.setBackgroundColor(Color.WHITE);
                          edit.setInputType(InputType.TYPE_CLASS_NUMBER);
                          edit.setHint(&quot;move to index&quot;);
                          edit.setLayoutParams(lp_edit);
  
                          Button button = new Button(MainActivity.this);
                          LinearLayout.LayoutParams lp_button= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                          button.setBackgroundColor(Color.BLUE);
                          button.setText(&quot;Move&quot;);
                          button.setLayoutParams(lp_button);
  
                          linear_parent.addView(linearLayout);
  
                          linearLayout.addView(imageView);
                          linearLayout.addView(edit);
                          linearLayout.addView(button);
               
                          button.setOnClickListener(new View.OnClickListener() {
                             @Override
                             public void onClick(View view) {
                                 String indexTarget_temp = edit.getText().toString();
                                 int indexTarget = Integer.parseInt(indexTarget_temp );
  
                                 int childCount = linear_parent.getChildCount();
                                 int currentLinearLayoutPos = linear_parent.indexOfChild(linearLayout);
                                 
                                 try {
                                     linear_parent.addView(linearLayout, indexTarget - 1); //adding current linearLayout to indexTarget  
  //error occurs this line
                           
                              linear_parent.removeView(linear_parent.getChildAt(currentLinearLayoutPos + 1)); //removing linearLayout at old index
                                 }
                                 catch(IllegalStateException e) {
                                     e.printStackTrace();
                                 }
                                 
                             }
                          });
                          
                      }
  
                  }else {
                      Toast.makeText(MainActivity.this, &quot;Enter index no&quot;, Toast.LENGTH_SHORT).show();
                  }
              }
          });
      }
  }
  </code></pre>
  <p>The error I'm getting is<br />
  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.<br />
  Any help would be highly appreciated.</p>