title: Android layout placeholder for dynamically added view
body: <p>In my Android Activity, I need to add a single View dynamically (at runtime), at a specific position in a layout. The view to be added is determined at runtime. For example, may layout may look something like this:</p>
  
  <pre><code>&lt;LinearLayout ...&gt;
      &lt;TextView ... /&gt;
      &lt;!-- Dynamic item to be added here --&gt;
      &lt;TextView ... /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>What is the best way to achieve this?</p>
  
  <p>One solution is to use <code>layout.addView(view, index)</code>, but I'd prefer not to hardcode the index.</p>
  
  <p>Another solution is to use a FrameLayout as a placeholder, and place may dynamic view inside the FrameLayout, for example:</p>
  
  <pre><code>&lt;LinearLayout ...&gt;
      &lt;TextView ... /&gt;
      &lt;FrameLayout android:id="@+id/placeholder" /&gt;
      &lt;TextView ... /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Then:</p>
  
  <pre><code>findViewById(R.id.placeholder).addView(view);
  </code></pre>
  
  <p>However, this adds an unnecessary view in the hierarchy.</p>
  
  <p>What would be the recommended way to do this?</p>