Home > Net >  Get value or checked value in Checkbox & RadioButton - Fastest way?
Get value or checked value in Checkbox & RadioButton - Fastest way?

Time:01-06

I'm currently working a Survey App where user choose or fill up some form with CheckBoxes and RadioButton and it will save in Firebase Real-Time database eventually once the 'Finish' button is clicked. Now I have this lines of code where I need to duplicate this type of codes in order for me to recognize which RadioGroup and RadioButton I need to read and save. It's like a loop and lengthy process for me.

enter image description here

On these line (in switch), I'm going to duplicate this until RadioGroup thirteenG, and I know it's kinda frustrating for me.

enter image description here

This will be the result once the button is clicked.

enter image description here

Now my problem is not an actual problem, I just wanna know and need a help on how do I make it shortcut or make it easy so that I wouldn't duplicate it anymore, you know what I mean, so that I will also save time and a line of code.

In short, the fastest way to read a CheckBox and RadioButton value within the RadioGroup.

Thank you so much, any help will be much appreciated. Thanks again and have a good day!

I have only tried case instead of else.

CodePudding user response:

checkbox : You can call isChecked() on a checkbox to get its status.

if(checkbox.isChecked){
//checkbox checked
} else {

}

radiogroup:

// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();

// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);

// and you can get value from radioButton using radioButton.getText
Toast.makeText(MyAndroidAppActivity.this,
       radioButton.getText(), Toast.LENGTH_SHORT).show();
  • Related