Search code examples
javaandroidandroid-checkboxandroid-radiobutton

RadioButton and Checkbox


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.


Solution

  • 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);
                }
            }
        });
    }