title: How to add a fragment to a programmatically generated layout?
body: <p>I have the following code working which generates fragments, but only if I am adding them to a linear layout which exists in my XML file.</p>
  
  <pre><code>LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments);
  FragmentManager fragMan = getFragmentManager();
  FragmentTransaction fragTransaction = fragMan.beginTransaction();
  
  Fragment myFrag= new ImageFragment();
  fragTransaction.add(R.id.foodItemActvity_linearLayout_fragments, myFrag , "fragment" + fragCount);
  fragTransaction.commit();
  </code></pre>
  
  <p>Now what if I want to add that fragment to a linear layout that does not already exist in the XML file such as</p>
  
  <pre><code>LinearLayout rowLayout = new LinearLayout();
  </code></pre>
  
  <p>Part 2:</p>
  
  <pre><code>    Fragment frag1 = generateAppropriateFragment(type1);
      Fragment frag2 = generateAppropriateFragment(type2);
  
      LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments);
      LinearLayout rowLayout = new LinearLayout(this);
      rowLayout.setId(12345); // add counter to end
  
      fragmentsLayout.addView(rowLayout);     
      getFragmentManager().beginTransaction().add(rowLayout.getId(), frag1, "fragment_grandchild" + fragCount).commit();
      fragCount++;
      getFragmentManager().beginTransaction().add(rowLayout.getId(), frag2, "fragment_grandchild" + fragCount).commit();
      fragCount++;
  </code></pre>