title: Android: using fragments programmatically
body: <p>For some reasons I can't use xml layout files.</p>
  
  <p>But I need to create a tablet android app.</p>
  
  <p>I decided to use fragments.</p>
  
  <p>I want to create the same layout that this xml generates:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="fill_parent" android:layout_height="fill_parent"&gt;
  
      &lt;fragment class="com.example.ItemsList"
              android:id="@+id/items_list" android:layout_weight="1"
              android:layout_width="0dp" android:layout_height="fill_parent" /&gt;
  
      &lt;FrameLayout android:id="@+id/item_details" android:layout_weight="1"
              android:layout_width="0dp" android:layout_height="fill_parent"
              android:background="?android:attr/detailsElementBackground" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>But I have problems with adding fragments to my linearLayout:</p>
  
  <pre><code>    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(createUI());
      }
  
      private View createUI() {
          LinearLayout layout = new LinearLayout(this);
  
          layout.setOrientation(LinearLayout.HORIZONTAL);
          layout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT));
          layout.setId(0x101);
          {
              FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
              fragmentTransaction.add(0x101, new ItemsList());
              fragmentTransaction.add(0x101, new ItemDetails());
              fragmentTransaction.commit();
          }
          return layout;
      }
  </code></pre>
  
  <p>Actually I can't even create LinearLayout with two identical fragments:</p>
  
  <pre><code>            ...
              fragmentTransaction.add(0x101, new ItemsList());
              fragmentTransaction.add(0x101, new ItemsList());
              ...
  </code></pre>
  
  <p>Please help</p>
  
  <p>Btw I can't figure out why do we need to declare "FrameLayout" for itemDetails Fragment but "fragment" is enough for itemsList ListFragment?</p>
  
  <p>UPD:</p>
  
  <p>To do so one should just add third param:</p>
  
  <pre><code>            ...
              fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag1");
              fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag2");
              ...
  </code></pre>
  
  <p>Default value for the tag parameter is null, so I was trying to create two different elements with identical ids. Thanks to p-lo for his comment.</p>