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
android android-radiogroup android-radiobutton
F_x
source share