title: How to Correctly Extend LinearLayout to Create a Custom View
body: <p>I have some "card", which is a simple LinearLayout with a TextView inside</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card="http://schemas.android.com/apk/res-auto"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
  &gt;
   &lt;TextView
          android:id="@+id/card_label_txt"
          android:layout_width="wrap_content"
          android:text="label" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>then I have my Main Fragment with a vertical LinearLayout.. and in this main fragment I add this "cards" to the main layout:</p>
  
  <pre><code># main fragment layout
  View view = inflater.inflate(R.layout.main_activity, null);
  LinearLayout ll = (LinearLayout) view
                  .findViewById(R.id.main_activity_ll);
  # get card
  View card = inflater.inflate(R.layout.card, null);
  
  # add to fragment layout
  ll.addView(card);
  </code></pre>
  
  <p>this works very good AND my card fills the whole width of fragment layout. Actually what I am expecting.</p>
  
  <p>Now I created a separate Class for my Card:</p>
  
  <pre><code>Class Card extends LinearLayout{
  
  public Card(Context context) {
          super(context);
  
          View view =  LayoutInflater.from(getContext()).inflate(
                  R.layout.card, null);
  
          this.addView(view);
  
      }
  }
  </code></pre>
  
  <p>And, then if I add my card to the main fragment layout in the way:</p>
  
  <pre><code># main fragment layout
  View view = inflater.inflate(R.layout.main_activity, null);
  LinearLayout ll = (LinearLayout) view
                  .findViewById(R.id.main_activity_ll);
  
  # add new Card to fragment layout
  ll.addView(new Card(getActivity());
  </code></pre>
  
  <p>then it is added BUT the width of the card is no more filled, but wraped to the textview.</p>
  
  <p>Could someone please explain me why I get different width sizes by this two method of adding same layouts?</p>
  
  <p><strong>Solution</strong> here is changed Card class that solves this issue:</p>
  
  <pre><code>public Card(Context context) {
         super(context);
  
         LayoutInflater.from(getContext()).inflate(
                  R.layout.card, this);
      }
  }
  </code></pre>