From my understanding, to determine if a checkbox is "clicked" and find if it's checked or not, code such as the following can be used:
cb=(CheckBox)findViewById(R.id.chkBox1); cb.setOnCheckedChangeListener(this); public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { cb.setText("This checkbox is: checked"); } else { cb.setText("This checkbox is: unchecked"); } }
However, I am unable to work out the logic on how to do the above for a radiogroup.
Here is the xml for my RadioGroup:
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio1" android:checked="true" android:text="RadioButton1"> </RadioButton> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio2" android:text="RadioButton2" android:checked="true"> </RadioButton> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio3" android:text="RadioButton3"> </RadioButton> </RadioGroup>
Question: Do i need to setup another listener, or will the listener already there also "register" this group?
Also, should the listener be set up on the RadioGroup or the RadioButton?
Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.
RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.
Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected.
Use the android:orientation="horizontal" -attribute of the RadioGroup.
This is how you get the checked radiobutton:
// This will get the radiogroup RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1); // This will get the radiobutton in the radiogroup that is checked RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());
To use the listener, you do this:
// This overrides the radiogroup onCheckListener rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // This will get the radiobutton that has changed in its check state RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId); // This puts the value (true/false) into the variable boolean isChecked = checkedRadioButton.isChecked(); // If the radiobutton that has changed in check state is now checked... if (isChecked) { // Changes the textview's text to "Checked: example radiobutton text" tv.setText("Checked:" + checkedRadioButton.getText()); } } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With