I add a RadioButton in my layout.
It is unchecked to begin with. And when I click on it , it becomes checked (as shown in emulator). But when when i click on it again, it does not become unchecked again?
<RadioButton android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/option1"/>
Solution is set checked[true/false] intermediate in Java code
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
When using radios you usually have more than one and choose between them. Like easy, medium, hard.
That is a conceptual question: Radio buttons allow you to choose between several options (represented by the other radio buttons). Typically, one radio button out of a group is always checked, even if in an initial state no buttons may be checked if you do not declare one as a default value. That means also that a single button does not allow to toggle its state unless other radio buttons are present in the same group - if there is only one option, you will have to choose it.
If you want a binary toggle, you will want to use a checkbox instead.
If you're looking for checkbox behavior with radio button appearance, you could pass in the xml style to a checkbox.
This can be useful in some cases (ex. using radio buttons in a RecyclerView) but you should be careful because the user expects radio buttons to behave a certain way. If you're allowing the user to make multiple selections you should probably use a normal checkbox, as mentioned in the comments above.
Hope this helps!
After searching a lot on SO, I came up with a not-so-good but decent workaround.
Declare a
boolean
variable for each RadioButton you have, initialize it withfalse
, and change the state of the variable and the RadioButton at every click.I know it's ideal to use a Checkbox, but if someone needs the radiobutton, then they need the radiobutton.
This is not an optimal solution, as it will basically toggle the button twice every time the user clicks the button (one is the default behaviour, the second time is inside your onclick function), so if you're using an
OnCheckedChangeListener
you will probably get two calls for the same click.There's another workaround, which is to change the
android:button
in a checkbox to another drawable with an xml template, but it's a bit more complex, requires at least 2 more files for the states.If you are having more than 1 radio buttons to work with then add "RadioGroups" as follows:
Have a look at this example , i am sure this will make your idea clear regarding Radio Buttons.
Also refer this Android Developer - Form Stuff page .