Unchecking a radio button [duplicate]
Asked Answered
L

2

4

The application is a step sequencer application with 16 radio groups with 8 buttons in each group. It works perfectly except once a group has a button selected I cant turn it off unless I use the clear button I have created to clear all radiogroups. What I would like to add is some code that says when a selected radio button is selected again it simply turns off like a toggle. I tried using toggles but then other issues arose with that method. Below is an attempt at it but it is way off the mark Im guessing

final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);

Button D1 = (Button)findViewById(R.id.RadioButtonD1);
        D1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PdBase.sendFloat("D1", 74);
                int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
                RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
                if(radioGroup1 != null) // This will be null if none of the radio buttons are selected
                       radioGroup1.clearCheck(); 
                PdBase.sendFloat("D1", 0);
            }
        });
Lately answered 4/4, 2013 at 20:43 Comment(0)
H
1

You need to put the button in a radio group, then clear the group. A radio button can't be unchecked directly, because the idea is that one option in a group is always checked.

Halfbeak answered 4/4, 2013 at 20:47 Comment(4)
It is in a radio group sorry and I have a button that does clear all of the buttons but that is for all radio groups. Actually I'll try and use that code more specifically for each group as well.Lately
Actually this wont work for me as I need to be able to switch within the radiogroup to different buttonsLately
Given complete code... Now when the application loaded it will reset and make sure that none of the radiobuttons is selected. Once the application is loaded in my system I am able to select the file.Shovel
I disagree that "the idea [of a radio group] is that one option in a group is always checked." The idea is to enforce mutual exclusion. No more than one option can be selected at a time but not having a selection is still perfectly valid. Groups have no selection by default and as you mentioned, a group's selection can be cleared programmatically (as the result of pressing a button for example.) The question is merely asking for a way to clear a group as the result of a different action - tapping a selected <RadioButton> button instead of a adding a "clear" <Button>. See my solution.Ebonieebonite
S
1

Here is an example of how to create radiogroup, radiobutton and how to use in Java code. Hope it will give you complete picture

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <RadioGroup
        android:id="@+id/maptype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/line1"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/map"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:drawableRight="@drawable/ic_launcher"/>

        <RadioButton
            android:id="@+id/satellite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_launcher"/>
    </RadioGroup>

</LinearLayout>

In Java Code

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    RadioGroup rgrpMapType = (RadioGroup) findViewById(R.id.maptype);
    int selectedTypeId = rgrpMapType.getCheckedRadioButtonId();
    RadioButton rbMapType = (RadioButton) findViewById(selectedTypeId);
    if(rbMapType != null) // This will be null if none of the radio buttons are selected
           rgrpMapType.clearCheck(); 
}
Shovel answered 4/4, 2013 at 20:51 Comment(3)
After line 3 in the above java code write this code. rbMapType will be null only if some radio button is selected. Hope the code it clear. if(rbMapType != null) rgrpMapType.clearCheck();Shovel
Thanks for the reply but it wont let me select the radio buttons when I implement the code you gave me this is how it looks after adding your codeLately
Couldnt get a fix so Im just going to add a clear button for each groupLately

© 2022 - 2024 — McMap. All rights reserved.