Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set only one RadioButton Can be selected at the time in RadioGroup

I've created a radio button in radiogroup, but when I try running the apps all radio button can be selected all the time, and how to set only one radiobutton can be selected at one time?

I'm using Fragment

RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);         radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {              @Override             public void onCheckedChanged(RadioGroup group, int checkedId) {                 // find which radio button is selected                 if(checkedId == R.id.Abdominal) {                     Toast.makeText(getActivity().getApplicationContext(), "choice: A",                             Toast.LENGTH_SHORT).show();                 } else if(checkedId == R.id.Arm) {                     Toast.makeText(getActivity().getApplicationContext(), "choice: B",                             Toast.LENGTH_SHORT).show();                 } else if(checkedId == R.id.Back){                     Toast.makeText(getActivity().getApplicationContext(), "choice: C",                             Toast.LENGTH_SHORT).show();                 } else if(checkedId == R.id.Chest){                     Toast.makeText(getActivity().getApplicationContext(), "choice: D",                             Toast.LENGTH_SHORT).show();                 } else if(checkedId == R.id.Leg){                     Toast.makeText(getActivity().getApplicationContext(), "choice: E",                             Toast.LENGTH_SHORT).show();                 } else if(checkedId == R.id.Shoulder){                     Toast.makeText(getActivity().getApplicationContext(), "choice: F",                             Toast.LENGTH_SHORT).show();                 }             }          }); 

here my xml code for RG and RB

<RadioGroup                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/RGroup">                      <TableRow android:weightSum="1">                     <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" />                         </TableRow>                     <TableRow>                         <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"/>                     </TableRow>                 </RadioGroup> 

EDITED 1 : Answer : If you dont want radio button can be selected in one time, so dont use Tablerow

like image 233
F_X Avatar asked May 19 '16 03:05

F_X


People also ask

How many Radiobuttons in a frame can be selected at the same time?

Radio buttons are groups of buttons in which, by convention, only one button at a time can be selected.

How do I make sure only one radio button is selected in Android Studio?

You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.

How many radio buttons are selected in RadioGroup?

However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup . By grouping them together, the system ensures that only one radio button can be selected at a time.

How do I group radio buttons in eclipse?

Select all the buttons you want to group by pressing CTRL and the button. Right click on one of the buttons and select properties. In the ButtonGroup of the Properties, click on the down arrow. Your selection of button groups should appear.


1 Answers

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. :)

like image 71
Janki Gadhiya Avatar answered Sep 23 '22 01:09

Janki Gadhiya