title: Proper way override fixed size custom view
body: <p>I have a custom view with Yellow background. I plan to add a Red background <code>TextView</code>, with <strong>match_parent</strong> for both width and height on it. Here's what I had done.</p>
  
  <h2>MainActivity.java</h2>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          LinearLayout mainView = (LinearLayout)this.findViewById(R.id.screen_main);
          RateAppBanner rateAppBanner = new RateAppBanner(this);
          mainView.addView(rateAppBanner);
      }
  }
  </code></pre>
  
  <h2>RateAppBanner.java</h2>
  
  <pre><code>public class RateAppBanner extends LinearLayout {
  
      public RateAppBanner(Context context) {
          super(context);
  
          setOrientation(HORIZONTAL);
  
          LayoutInflater.from(context).inflate(R.layout.rate_app_banner, this, true);
  
          this.setBackgroundColor(Color.YELLOW);
      }
  }
  </code></pre>
  
  <h2>rate_app_banner.xml</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"&gt;
  
      &lt;TextView 
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textColor="#ffffffff"
          android:background="#ffff0000"
          android:text="Hello World" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/JLy8j.png" alt="enter image description here"></p>
  
  <p>Now, I would like to have a <strong>fixed width &amp; height</strong> custom view. I realize, after I'm having fixed width &amp; height custom view, the added <code>TextView</code> doesn't obey <strong>match_parent</strong> attribute.</p>
  
  <p>Here's the change I had done on custom view.</p>
  
  <h2>RateAppBanner.java</h2>
  
  <pre><code>public class RateAppBanner extends LinearLayout {
      ...
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          int desiredWidth = 320;
          int desiredHeight = 50;
  
          desiredWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, desiredWidth, getResources().getDisplayMetrics());
          desiredHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, desiredHeight, getResources().getDisplayMetrics());
  
          super.onMeasure(desiredWidth, desiredHeight);
          //MUST CALL THIS
          setMeasuredDimension(desiredWidth, desiredHeight);
      }
  </code></pre>
  
  <p>I realize the added <code>TextView</code> doesn't <strong>match_parent</strong> anymore!</p>
  
  <p><img src="https://i.stack.imgur.com/iYLSa.png" alt="enter image description here"></p>
  
  <p>Now, we can see the Yellow custom view is having fixed size 320x50. I expect the Red <code>TextView</code> will fill up the entire custom view due to <strong>match_parent</strong> attribute.</p>
  
  <p>However, that's not the case. I believe my implementation for custom view's <code>onMeasure</code> isn't correct. May I know what is the correct way to fix this?</p>
  
  <p>The complete source code can be downloaded from <a href="https://www.dropbox.com/s/gg8wv6fffqewygr/abc-fixed-size-problem.zip?dl=0" rel="noreferrer">abc.zip</a></p>