title: Android: Custom view from xml layout
body: <p>i would like to create a custom <code>View</code> (subclass of the <code>View</code> class) and use a layout xml resource.</p>
  
  <p>I want something like this:</p>
  
  <pre><code>public class CustomView extends LinearLayout {
  
      public CustomView(Context context) {
            super(context);
            LayoutInflater  mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mInflater.inflate(R.layout.tweet, this, true);
  }
  </code></pre>
  
  <p>This actually creates a <code>View</code> with the right height and width (a <code>ScrollView</code> with a list of those has exactly the expected length and scrollbars) but they are empty (black) even though the xml layout contains a lot of <code>TextViews</code>.</p>
  
  <p>This is my xml layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/tweet"
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"
      android:background="#ffffff"&gt;
      &lt;RelativeLayout
          android:layout_height="wrap_content"
          android:layout_width="match_parent"&gt;
          &lt;LinearLayout 
              android:layout_height="wrap_content" 
              android:layout_width="match_parent" 
              android:orientation="horizontal"
              android:background="#ffffff"&gt;
              &lt;TextView 
                  android:text="User" 
                  android:id="@+id/tvusername" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content"
                  android:textColor="#000000"
                  android:textStyle="bold"&gt;
              &lt;/TextView&gt;
              &lt;View
                  android:layout_width="5dip"
                  android:layout_height="1dip"&gt;           
              &lt;/View&gt;
              &lt;TextView 
                  android:text="userscreenname" 
                  android:id="@+id/tvuserscreenname" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content"&gt;
              &lt;/TextView&gt;    
          &lt;/LinearLayout&gt;
          &lt;TextView
              android:text="0s"
              android:id="@+id/tvtime"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"&gt;
          &lt;/TextView&gt;
      &lt;/RelativeLayout&gt;
      &lt;TextView 
          android:text="Tweet Content" 
          android:id="@+id/tvcontent" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"
          android:textColor="#000000"&gt;
      &lt;/TextView&gt;
      &lt;View 
          android:layout_width="match_parent"
          android:layout_height="1px"
          android:background="#ffffff"&gt;
      &lt;/View&gt;
      &lt;TextView 
          android:text="Retweet by" 
          android:id="@+id/tvrtby" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"&gt;
      &lt;/TextView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is my main layout:</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="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#ffffff"
      &gt;
  
  &lt;ScrollView 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:id="@+id/scrollView1" 
      android:orientation="vertical"
     android:layout_alignParentTop="true"&gt;
      &lt;LinearLayout 
          android:layout_width="match_parent" 
          android:id="@+id/timeline" 
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:background="#ffffff"&gt;           
              &lt;TextView  
              android:id="@+id/tvoutput"
              android:layout_width="match_parent" 
              android:layout_height="wrap_content" 
              android:text="@string/hello"
              /&gt;    
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is how I add the custom view to my main view:</p>
  
  <p>Not working (what I try to achieve):</p>
  
  <pre><code>TweetView ctweet = new TweetView(getApplicationContext(),tweet);
  timeline.addView(ctweet);
  </code></pre>
  
  <p>My current solution (works, but uses no custom view):</p>
  
  <pre><code>            View vtweet = View.inflate(getApplicationContext(), R.layout.tweet,null);
  
              TextView tvusername = (TextView) vtweet.findViewById(R.id.tvusername);
              TextView tvuserscreenname = (TextView) vtweet.findViewById(R.id.tvuserscreenname);
              TextView tvcontent = (TextView) vtweet.findViewById(R.id.tvcontent);
              TextView tvtime = (TextView) vtweet.findViewById(R.id.tvtime);
  
              tvusername.setText(tweet.getUser().getName());
              tvuserscreenname.setText('@' + tweet.getUser().getScreenName());
              tvcontent.setText(tweet.getText()); 
              //tvtime.setText(ctweet.tvtime.getText()); 
              timeline.addView(vtweet);
  </code></pre>