title: Adding multiple fragments to LinearLayout
body: <p>I want to add multiple fragments to my LinearLayout by clicking a button.</p>
  
  <p>This is my MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button addFragmentButton = (Button) findViewById(R.id.addFragmentButton);
          addFragmentButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  FragmentManager fragmentManager = getFragmentManager();
                  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                  HeaderButtonFragment fragment = new HeaderButtonFragment();
                  Integer x = (int) (Math.random() * 1000);
                  String name = String.valueOf(x);
                  fragmentTransaction.add(R.id.fragmentsHolder, fragment, name);
                  fragmentTransaction.commit();
              }
          });
      }
  }
  </code></pre>
  
  <p>my main layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      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"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="majewski.ninja.mylibrary.MainActivity"&gt;
  
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:id="@+id/fragmentsHolder"&gt;
      &lt;/LinearLayout&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Add new fragment"
          android:id="@+id/addFragmentButton"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>My Fragment:</p>
  
  <pre><code>public class HeaderButtonFragment extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          Integer x = (int) (Math.random() * 1000);
          String name = String.valueOf(x);
          View view =  inflater.inflate(R.layout.fragment_header_button, container, false);
          ((Button) view.findViewById(R.id.fragmentButton)).setText(name);
          return view;
      }
  }
  </code></pre>
  
  <p>and fragment layout:</p>
  
  <pre><code>&lt;FrameLayout 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"
      tools:context="majewski.ninja.mylibrary.HeaderButtonFragment"&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New Button"
          android:id="@+id/fragmentButton"
          android:layout_gravity="left|top" /&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>This is what I get when I click the button:</p>
  
  <p><a href="https://i.stack.imgur.com/08XnU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/08XnU.png" alt="enter image description here"></a></p>
  
  <p>When I click it more times nothing changes but I want it to add new fragments so it can look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/UdMD1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UdMD1.png" alt="enter image description here"></a></p>
  
  <p>Is there a way I can do that?</p>