title: Custom Viewgroup works inside LinearLayout not RelativeLayout
body: <p>I wanted to create a custom LinearLayout (and later a custom ImageButton) that could take percentage values for both dimensions of size based on its parent's size regardless of the parent type (Relative or Linear). I was following this post: <a href="https://stackoverflow.com/questions/2159320/how-to-size-an-android-view-based-on-its-parents-dimensions">How to size an Android view based on its parent&#39;s dimensions</a>, and it was very helpful, but I have a problem that those answers don't address.</p>
  
  <p>When I place my Custom LinearLayout inside another LinearLayout, everything works as expected. My Custom LinearLayout covers the expected space (80% of the parent's width in the example below).</p>
  
  <p>However if I place it inside a RelativeLayout, my screen always shows empty, I am not sure why this happens.</p>
  
  <p>Here is my class:</p>
  
  <pre><code>public class ButtonPanel extends LinearLayout {
  
      public ButtonPanel(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  
          int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
          int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
  
          int widthMode = MeasureSpec.getMode(widthMeasureSpec);
          int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  
          int newWidth = (int) Math.ceil(parentWidth * 0.8);
  
          this.setMeasuredDimension(newWidth, parentHeight);
          this.setLayoutParams(new RelativeLayout.LayoutParams(newWidth,parentHeight));
  
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      }
  }
  </code></pre>
  
  <p>And here is my testing layout for the activity.</p>
  
  <pre><code>&lt;RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"&gt;
  
          &lt;com.android.tests.views.ButtonPanel
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/inner_panel"
                  android:gravity="center_horizontal"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"&gt;
          &lt;/com.android.tests.views.ButtonPanel&gt;
      &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>In my activity all I do is set the Content View to the above layout.</p>
  
  <p>(Incidentally, does anybody now how I could get the type of the parent dynamically for setting the new LayoutParameters? Above you'll see the parent type (RelativeLayout) hard-coded into the Custom View onMeasure function)</p>
  
  <p>Thanks in advance for any help! </p>