I have an app that uses radio buttons. The default for this button is set in the main.xml file, ie:
android:id="@+id/rb_sat1E"
android:checked="true"
In the Java file I have:
final RadioButton radio1 = (RadioButton)findViewById(R.id.rb_sat1E);
I have also created a 'Reset' button in the main Java file and can use the following code to reset TextView information ie.
pos1_deg.setText("0.0");
But how do I reset a radio button? I would have thought it to be something like
radio1.setBoolean("TRUE");
But that does not work at all.
Any help greatly appreciated. Thanks.
Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead. Scroll down and see checkbox and toggle button. When using radios you usually have more than one and choose between them.
here you go. Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected.
For radioButton use
radio1.setChecked(true);
It does not make sense to have just one RadioButton. If you have more of them you need to uncheck others through
radio2.setChecked(false); ...
If your setting is just on/off use CheckBox.
If you want to do it in code, you can call the check member of RadioGroup:
radioGroup.check(R.id.radioButtonId);
This will check the button you specify and uncheck the others.
Or you can do it in the XML file :
In the RadioGroup using : android:checkedButton="id_button_to_check"
or in the RadioButton : android:checked="true"
Just to clarify this: if we have a RadioGroup
with several RadioButtons
and need to activate one by index, implies that:
radioGroup.check(R.id.radioButtonId)
and
radioGroup.getChildAt(index)`
We can to do:
radioGroup.check(radioGroup.getChildAt(index).getId());
if you have done the design in XML and want to show one of the checkbox in the group as checked when loading the page below solutions can help you
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLastNameSignUp"
android:layout_margin="20dp"
android:orientation="horizontal"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="@+id/Male"
android:text="Male"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Female"
android:text="Female"/>
</RadioGroup>
Many times if your radio buttons belong to the same radioGroup then
radioButton.setChecked(true)
will not select the radio button properly. So to solve this problem try using your radioGroup.
radioGroup.check(R.id.radioButtonId)
Use this code
((RadioButton)findViewById(R.id.radio3)).setChecked(true);
mistake -> don't forget to give () for whole before setChecked() -> If u forget to do that setChecked() is not available for this radio button
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