Home > front end >  How do I make sure that radiobuttons arent clickable if the checkbox is unchecked in android studio?
How do I make sure that radiobuttons arent clickable if the checkbox is unchecked in android studio?

Time:04-12

I'm practicing an exercise from a book, and it's asking me to first, create a checkbox and then three radiobuttons. If the checkbox is unchecked, then the radiobuttons should appear to be disabled/grey/unclickable. However, as soon as you check the checkbox, the radiobuttons should appear to be working and clickable. I created the layout for both the checkbox and the radiobuttons (radiobuttons are a part of radiogroup) but I'm having troubling connecting them in the way the exercise is asking. How would I go about writing this out in the mainactivity?

CodePudding user response:

You can use if statement for check your checkbox is already click or not. Set your button in condition disabled/grey first after that use if statement for check your checkbox, if checkbox have clicked usable your button.

your code will look like this

Button button1, button2, button3;
Checkbox checkbox;

button1 = findViewById (yourid);
button2 = findViewById (yourid);
button3 = findViewById (yourid);
checkbox = findViewById (yourid);

//Disable your button first
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);

if(checkbox.isChecked()){
  button1.setEnabled(true);
  button2.setEnabled(true);
  button3.setEnabled(true);
 }
else{
  Toast.makeText(getApplicationContext(), "Checkbox Haven't clicked", 
  Toast.LENGTH_SHORT).show();
 }
  • Related