Possible to allow all radio buttons to be unchecked in an Android app?
Asked Answered
M

2

5

Just wanted to see if anyone knows if there's a radiogroup or radiobutton attribute or something else quick that will allow radio buttons to be unchecked when they're in checked mode. I'm looking to build functionality that works like a radio group (i.e. only one can be checked) but I also want them to be able to be all unchecked.

Milling answered 22/6, 2011 at 2:27 Comment(0)
L
11

Maybe I don't get the question here, but this is something I wanted to do. I have a single activity that I use to classify many pictures. I am classifying using radio buttons. After the user checks one of the options he is allowed to switch with the next picture. I needed to clear the selection, when switching the picture, but decided not to create new activity.

So I initialize my radio group in the layout like that:

<RadioGroup
    android:id="@+id/radio_selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio_true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/true" />

    <RadioButton
        android:id="@+id/radio_false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/false" />
</RadioGroup>

This initializes my radio group and both RadioButton are not selected initially. Afterwards when I change the picture I need to clear the selection (because user has not yet chosen for the new picture). I do like that:

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_selection);
radioGroup.clearCheck();

This is doing exactly what I need: making again none of the radio buttons being selected. I hope I understood the question and this will help somebody in the future.

Langmuir answered 7/4, 2012 at 14:13 Comment(3)
thanks for trying to answer the question, but I asked it about 9 months ago. =) already figured out a solution myselfMilling
@Milling Yes I know it is very old question. I just add my own bit. The purpose of SO is not to ask question and when you figure out a solution keep it for yourself - just share here what you have found: xkcd.com/979Langmuir
@wajiw:If u got the answer,then u must share it.......that will be very good for all SO follower to this question...Maternity
A
3

You can use a CheckBox to mimic the functionality you want as shown below. The code assumes that you have two check boxes but you could have more than two.

public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.checkBox1) {
        // Toggle status of checkbox selection
        checkBox1Selected = checkBox1.isChecked();

        // Ensure that other checkboxes are not selected
        if (checkBox2Selected) {
            checkBox2.setChecked(false);
            checkBox2Selected = false;
         } 
    else if (id == R.id.checkBox2) {
         // Toggle status of checkbox selection
         checkBox2Selected = checkBox2.isChecked();

        // Ensure that other checkboxes are not selected
        if (checkBox1Selected) {
            checkBox1.setChecked(false);
            checkBox1Selected = false;
        }
}
Amling answered 22/6, 2011 at 3:1 Comment(2)
Yup, after reading through the docs I found out the only way to clear all radio buttons is to clear the radiogroup which wouldn't help me. I'm working inside a custom cursor adapter so this code won't work for me. Thanks for the suggestion though. I need to figure out how to access other checkboxes in a cursoradapter when one is clicked.Milling
@waj In that case, in your getView method setOnClickListener to the ChecKBoxes and keep track of how many checkBoxes are checked. Also, keep track of the checkbox that is currently checked. If the number of checkboxes that are checked are more than 1 then when the user selects another checkbox then uncheck the previously selected checkbox and check the new one. If the user unchecks an existing checkbox reset counter to 0. Hope that is not too confusing.Amling

© 2022 - 2024 — McMap. All rights reserved.