How to set only one RadioButton Can be selected at the time in RadioGroup
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Debit"
android:id="@+id/rDebit"
android:checked="false"
/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Credit"
android:id="@+id/rCredit"
android:checked="false" />
</RadioGroup>
And in java file
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
And when to do something
if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}
I have noticed that single selection does not work without setting id
to radio buttons.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/expenseRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/expense" />
<RadioButton
android:id="@+id/incomeRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/income" />
</RadioGroup>
It's not working because of TableRow inside RadioGroup. All RadioButtons are not grouped together because of TableRow between them.
RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.
Just change your code like this it will work :
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="@+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="@+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/Back" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="@+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="@+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="@+id/Shoulder"/>
</RadioGroup>
Hope this helps. :)