How to install only one RadioButton Can be selected at a time in RadioGroup

I created a radio button in a radio group, but when I try to run applications, all radio buttons can be selected all the time, and how can I install only one radio add-on, can I choose at a time?

I use 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 is 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 do not want the switch to be selected at a time, so do not use Tablerow

+15
android android-radiogroup android-radiobutton
source share
6 answers

It does not work due to TableRow inside RadioGroup. All RadioButtons are not grouped due to the TableRow between them.

RadioButton must be a direct child of RadioGroup, otherwise the grouping does not work.

Just change your code as if it would 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. :)

+20
source share
  <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 } 
+5
source share

The easy way. onclick switch. execute the code as shown below.

 public void clearRadioChecked() { rdopa.setChecked(false); rdopb.setChecked(false); rdopc.setChecked(false); rdopd.setChecked(false); } 

if you selected rdopa, then press the rdopa button as shown below.

 clearRadioChecked() rdopa.setChecked(true); 
+4
source share

You can use the android:checkedButton in the RadioGroup by providing the RadioButton id that you want to check first, and selecting another RadioButton will clear the previous selection.

 <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:checkedButton="@+id/rbNo" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="50dp" android:text="Yes" /> <RadioButton android:id="@+id/rbNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="No" /> </RadioGroup> 
0
source share

You can create your own RadioGroup , which can find all the RadioButton that are nested in your group, whether the child is direct or not. Below is the code of my NestedRadioGroup . Using this instead of your RadioGroup in your XML file should help.

 public class NestedRadioGroup extends LinearLayout { private SparseArray<RadioButton> radioButtons; private int checkedId; public NestedRadioGroup(Context context) { this(context, null); } public NestedRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); radioButtons = new SparseArray<>(); checkedId = -1; } @Override public void addView(View child, int index, ViewGroup.LayoutParams params) { super.addView(child, index, params); findRadioButtons(child); } private void findRadioButtons(View child) { if (child instanceof RadioButton) { RadioButton newButton = (RadioButton) child; newButton.setId(radioButtons.size()); newButton.setOnCheckedChangeListener((buttonView, isChecked) -> { if (isChecked) { if (checkedId != -1) { radioButtons.get(checkedId).setChecked(false); } radioButtons.get(buttonView.getId()).setChecked(true); checkedId = buttonView.getId(); } }); radioButtons.put(newButton.getId(), newButton); } else if (child instanceof ViewGroup) { ViewGroup group = (ViewGroup) child; for (int i = 0; i < group.getChildCount(); i++) { this.findRadioButtons(group.getChildAt(i)); } } } } 

Any ideas to make the code cleaner?

I hope you succeed :)

0
source share

I ran into the same problem right now and found where I was making mistakes, and I describe it below:

1) If you use RadioButtons in RadioGroup, then RadioButtons must be a direct descendant of RadioGroup.

2) I checked my first condition, and it was correct, but still all the switches are selected, and when it gives the identifiers and starts the project, it automatically solves my problem.

I hope so .. my answer will help you!

0
source share

All Articles