-3

I have 3 radio buttons and 10 checkboxes, I just want to prevent the user from choosing any checkboxes until it chooses one of the radio buttons first, please help.

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Ramlawi
  • 11
  • 4

4 Answers4

1

Add RadioButton inside radio group.

 <RadioGroup
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/radio_group"
 >
 <RadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/button1"
     android:text="btn 1"
     />
 <RadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="btn 2"
     android:id="@+id/button2"
     />
 <RadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/button3"
     android:text="btn 3"
     />
</RadioGroup>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radio_group"
    android:layout_marginTop="10dp"
    android:orientation="vertical"
    >

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check1"
        android:text="check 1"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check2"
        android:text="check 2"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check3"
        android:text="check 3"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check4"
        android:text="check 4"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check5"
        android:text="check 5"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check6"
        android:text="check 6"
        />


</LinearLayout>

And implement the RadioGroup onCheckChangeListener inside activity. before that make sure to disable all checkbox by checkbox.setEnabled(false)

public class MainActivity extends AppCompatActivity {


RadioGroup radioGroup;
RadioButton radioButton1,radioButton2,radioButton3;
CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup= (RadioGroup) findViewById(R.id.radio_group);
    radioButton1= (RadioButton) findViewById(R.id.button1);
    radioButton2= (RadioButton) findViewById(R.id.button2);
    radioButton3= (RadioButton) findViewById(R.id.button3);
    checkBox1= (CheckBox)findViewById(R.id.check1);
    checkBox2= (CheckBox)findViewById(R.id.check2);
    checkBox3= (CheckBox)findViewById(R.id.check3);
    checkBox4= (CheckBox)findViewById(R.id.check4);
    checkBox5= (CheckBox)findViewById(R.id.check5);
    checkBox6= (CheckBox)findViewById(R.id.check6);


    checkBox1.setEnabled(false);
    checkBox2.setEnabled(false);
    checkBox3.setEnabled(false);
    checkBox4.setEnabled(false);
    checkBox5.setEnabled(false);
    checkBox6.setEnabled(false);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);

            boolean isChecked = checkedRadioButton.isChecked();
            if (isChecked){
                checkBox1.setEnabled(true);
                checkBox2.setEnabled(true);
                checkBox3.setEnabled(true);
                checkBox4.setEnabled(true);
                checkBox5.setEnabled(true);
                checkBox6.setEnabled(true);
            }
        }
    });
}
Raza
  • 791
  • 7
  • 22
0

By default set all Checkboxes as a disabled. Then add check change listener for all Radio buttons. Add code to enable all Checkboxes on Radio button check change listener.

Ajay Mehta
  • 843
  • 5
  • 14
0

Use RadioButtons methods, set Checkboxes as disabled and check if one of the radiobutton isSelected(), if true, set checkboxex enabled.

Mini Nihat
  • 38
  • 5
0

You can either keep one radio button as selected, by default so the user will have to go with the default selection or will change selection - both ways you always have one radio button selected.

If you do not want to keep a default radio selection,

  1. Use a radio group

    RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);

  2. Disable all the checkboxes in the beginning

    checkBox.setEnabled(false);

  3. Set onClick listener for radio group and enable checkBox inside.

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { checkBox.setEnabled(false); } });

Maria
  • 369
  • 2
  • 7