title: Need Help adding programmatically adding ToggleButton to a FrameLayout with half the layout_weight
body: <p>My goal is to dynamically add a toggleButton to a frameLayout (which has it's own weight), but have the button be half the width of the frameLayout. I am able to create what I want in XML, but I'm having trouble trying to make this work programmatically.</p>
  
  <p>Here's my XML:</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"&gt;
  
      &lt;LinearLayout
          android:id="@+id/columns"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:baselineAligned="false"
          android:orientation="horizontal"
          android:weightSum="8"&gt;
  
          &lt;FrameLayout
              android:id="@+id/column_1"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"&gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"&gt;
  
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="60dp"
                      android:background="@android:color/background_dark" /&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/buttonLayoutWrapper"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:weightSum="2"&gt;
  
                  &lt;ToggleButton
                      android:layout_width="0dp"
                      android:layout_height="360dp"
                      android:layout_weight="1"
                      android:checked="true"
                      android:text=""
                      android:textOff=""
                      android:textOn=""/&gt;
  
              &lt;/LinearLayout&gt;
  
          &lt;/FrameLayout&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Please pay particular attention to the LinearLayout with the id: buttonLayoutWrapper and it's child ToggleButton as those are what I need to add dynamically. For simplicity sake I'm only showing one column, but would repeat this for 7 more.</p>
  
  <p>I tried creating a separate class that Extends LinearLayout and adds the ToggleButton thinking that I could get the weight that way, but no luck.</p>
  
  <p>No matter what I do, I can't seem to get the ToggleButton to obey the layout_weight.</p>
  
  <p>Below is a sample of trying to do this programmatically:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      FrameLayout frameLayout = (FrameLayout) findViewById(R.id.column_1);
      ToggleButton toggleButton = new ToggleButton(this);
      LinearLayout.LayoutParams layoutWrapper = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 360, .5f);
      toggleButton.setChecked(true);
      toggleButton.setText("");
      toggleButton.setTextOn("");
      toggleButton.setTextOff("");
      toggleButton.setLayoutParams(layoutWrapper);
      frameLayout.addView(toggleButton);
  }
  </code></pre>
  
  <p>It seems that I need to have a parent LinearLayout that sets weightSum for the layout weight to work in the XML version. However, I can't seem to figure out how to add a LinearLayout parent to this that can then be added to the FrameLayout without getting a strange:</p>
  
  <blockquote>
    <p>cannot cast LinearLayout to FrameLayout</p>
  </blockquote>
  
  <p>error when compiling.</p>