title: creating custom views without an extra view hierarchy
body: <p>I usually create a custom view, for say,</p>
  
  <pre><code>public class MyView extends LinearLayout{
  
    public MyView(Context context){
       super(context);
  
       //Inflating my custom layout
       LayoutInflater inflater = getLayoutInflater();
       inflater.inflate(R.layout.view_layout, this); 
  
    }
  
  }
  </code></pre>
  
  <p>The problem with the above layout is, it will create a new <code>LinearLayout</code> and inflate <code>R.layout.view_layout</code> inside it, adding a new view hierarchy which is not preferred.</p>
  
  <p><code>R.layout.view_layout</code> contains <code>RelativeLayout</code> with many child elements, which I cannot add programatically by extending <code>RelativeLayout</code> (since positioning is difficult). </p>
  
  <p>Having unnecessary hierarchies slow down my app.</p>
  
  <p>What is the best way to create <code>custom views/view group</code> without an extra hierarchy level.</p>
  
  <blockquote>
    <p><strong>Solved as per the soln by CommonsWare:</strong></p>
  </blockquote>
  
  <p>XML Layout:</p>
  
  <pre><code>&lt;merge&gt;
  
  &lt;TextView ...
  .../&gt;
  
  More items
  
  &lt;/merge&gt;
  </code></pre>
  
  <p>Since the XML layout had <code>RelativeLayout</code> as the parent view, I will extend <code>RelativeLayout</code> in the code instead of <code>LinearLayout</code></p>
  
  <pre><code>public class MyView extends RelativeLayout{
  
        public MyView(Context context){
           super(context);
  
           //Inflating my custom layout
           LayoutInflater inflater = getLayoutInflater();
           inflater.inflate(R.layout.view_layout, this); 
  
        }
  
      }
  </code></pre>