user2008006
user2008006

Reputation: 37

Joining Radiogroups

Is there a way to join to radio groups together, so when a button is clicked from group a, the button from group b is unclicked

     <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_below="@id/NFCHeaderTextView"
                    android:layout_width="fill_parent"
                    android:background="@drawable/back"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:id="@+id/hello">

   <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:background="@drawable/back"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:id="@+id/RadioGroup">
    </RadioGroup>
    <TextView android:id="@+id/Savings"
                android:background="@drawable/two_tone_green_button_selector"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="16dp"
                android:gravity="left|center_vertical"
                android:textColor="@color/two_green_button_color_selector"
                android:onClick="onRadioButtonClicked"
                android:text="@string/Savings"/>

   <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:background="@drawable/back"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:id="@+id/SavingsR">
    </RadioGroup>

   <TextView android:id="@+id/Credit"
                android:background="@drawable/two_tone_green_button_selector"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="16dp"
                android:gravity="left|center_vertical"
                android:textColor="@color/two_green_button_color_selector"
                android:onClick="onRadioButtonClicked"
                android:text="@string/Credit"/>

   <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:background="@drawable/back"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:id="@+id/CreditR">
    </RadioGroup>
    </RadioGroup>
    </RelativeLayout>

I have this code, but when I run it, I can still click two buttons from two different groups, I am using java in eclipse

Upvotes: 1

Views: 378

Answers (1)

Tobrun
Tobrun

Reputation: 18391

EDIT:

I quickly wrote some sample code, In this example we have two different radiogroups but only one radiobutton of the two groups can be checked. We are using a OnCheckedChangeListener to capture events relating to changing states of radiobuttons inside a RadioGroup. This is some sample code and is not the correct way of handling OnCheckedChangeListener callbacks, I just wanted to show you an example with two radiogroups. I hope this is helpfull.

JAVA:

    final RadioGroup group1 = (RadioGroup) findViewById(R.id.radiogroup1);
    final RadioGroup group2 = (RadioGroup) findViewById(R.id.radiogroup2);

    group1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            ((RadioButton)group2.findViewById(R.id.option4)).setChecked(false);
            ((RadioButton)group2.findViewById(R.id.option5)).setChecked(false);
            ((RadioButton)group2.findViewById(R.id.option6)).setChecked(false);
        }
    });

    group2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            ((RadioButton)group1.findViewById(R.id.option1)).setChecked(false);
            ((RadioButton)group1.findViewById(R.id.option2)).setChecked(false);
            ((RadioButton)group1.findViewById(R.id.option3)).setChecked(false);
        }
    });

XML:

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

    <RadioButton
        android:id="@+id/option1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 1" />

    <RadioButton
        android:id="@+id/option2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 2" />

    <RadioButton
        android:id="@+id/option3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 3" />
</RadioGroup>

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

    <RadioButton
        android:id="@+id/option4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 4" />

    <RadioButton
        android:id="@+id/option5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 5" />

    <RadioButton
        android:id="@+id/option6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 6" />
</RadioGroup>

ORIGINAL:

Use an OnCheckedChangeListener, This listener get's called when a Radio state changes. With this functions you can loop the other RadioGroup to uncheck the other radiobuttons.

Hope this helps,

if not comment below

Upvotes: 1

Related Questions