title: How can I add radio buttons to a radio group I've made programmatically?
body: <p>I am completely new to Android</p>
  
  <p>i tried whatever I thought may work but everytime I get error. What's wrong here?</p>
  
  <p><strong>main_activity.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="16dp"
      tools:context=".Main"
      android:orientation="vertical"
      android:id="@+id/mainLayout"&gt;
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton1" /&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton2" /&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton3" /&gt;
      &lt;/LinearLayout&gt;
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton4" /&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton5" /&gt;
  
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton6" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>public class Main extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main_activity);
  
          RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
          RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
          RadioButton rb3 = (RadioButton) findViewById(R.id.radioButton3);
          RadioButton rb4 = (RadioButton) findViewById(R.id.radioButton4);
          RadioButton rb5 = (RadioButton) findViewById(R.id.radioButton5);
          RadioButton rb6 = (RadioButton) findViewById(R.id.radioButton6);
  
          RadioGroup rg = new RadioGroup(this);
          rg.addView(rb1);
          rg.addView(rb2);
          rg.addView(rb3);
          rg.addView(rb4);
          rg.addView(rb5);
          rg.addView(rb6);
  
      }
  
  }
  </code></pre>
  
  <p>in fact I am trying to make a radio group with 3 columns and 2 rows but as I tried it doesn't work when I do it like this:</p>
  
  <p><strong>main_activity.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="16dp"
      tools:context=".Main"
      android:orientation="vertical"
      android:id="@+id/mainLayout"&gt;
    &lt;RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"&gt;
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton1" /&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton2" /&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton3" /&gt;
      &lt;/LinearLayout&gt;
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton4" /&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton5" /&gt;
          &lt;RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="New RadioButton"
              android:id="@+id/radioButton6" /&gt;
      &lt;/LinearLayout&gt;
   &lt;/RadioGroup&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>the last solution I got is to make a radio group in <code>Main.java</code> programmatically but it makes an error on <code>rg.addView()</code> method</p>