Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only one radio button selected at a time

I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?

public void onRadioButtonClick(View v)
{
    RadioButton button = (RadioButton) v;
    boolean checkBox1Selected;
    boolean checkBox2Selected = false;

    Toast.makeText(MainActivity.this,
        button.getId()+ " was chosen."+R.id.wificheckBox,
        Toast.LENGTH_SHORT).show();

    if ( button.getId() ==R.id.wificheckBox) {
        // Toggle status of checkbox selection
        checkBox1Selected = radiowifiButton.isChecked();

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

main xml

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_1"
    android:layout_marginLeft="20dp">

<LinearLayout
    android:id="@+id/ll_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_1"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/wifimobilecheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/button_radio"
        android:checked="true"
        android:onClick="onRadioButtonClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/wificheckBox"
        android:layout_toRightOf="@+id/wificheckBox"
        android:paddingLeft="15dp"
        android:text="WiFi or mobile network"
        android:textColor="#333333"
        android:textSize="20dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_2"
    android:paddingTop="20dp"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/wificheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/button_radio"

        android:onClick="onRadioButtonClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/wificheckBox"
        android:layout_toRightOf="@+id/wificheckBox"
        android:paddingLeft="15dp"
        android:text="WiFi "
        android:textColor="#333333"
        android:textSize="20dp" />
</LinearLayout>

</RadioGroup>

Drawabe- button_radio

<item android:state_checked="true"android:state_pressed="false" android:drawable="@drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="@drawable/checkbox"/>
like image 439
user182192 Avatar asked Apr 11 '12 14:04

user182192


People also ask

How many radio buttons can be selected at once?

Radio buttons are groups of buttons in which, by convention, only one button at a time can be selected.

How do I select one radio button at a time?

Radio buttons are normally presented in radio groups (a collection of radio buttons describing a set of related options). Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group.

Does radio button allowing multiple selections?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name. Radio buttons have Default styling.


2 Answers

If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.

like image 186
Bill Gary Avatar answered Nov 15 '22 05:11

Bill Gary


As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.

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

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:button="@drawable/selector_radio_button"
                android:text="Female" />

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/selector_radio_button"
                android:text="Male" />
</RadioGroup>

Setting the android:checkedButton="@+id/rb_female" worked for me in RadioGroup.

like image 20
Bilal Haider Avatar answered Nov 15 '22 06:11

Bilal Haider