I am completely new to Android
i tried whatever I thought may work but everytime I get error. What's wrong here?
main_activity.xml
<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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton1" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton2" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton4" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton5" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton6" />
</LinearLayout>
</LinearLayout>
Main.java
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);
}
}
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:
main_activity.xml
<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">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton1" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton2" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton4" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton5" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton6" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
the last solution I got is to make a radio group in Main.java
programmatically but it makes an error on rg.addView()
method