Question

I have 3 radio groups in the same layout and fragment. They are defined as:

<RadioGroup
        style="@style/AppRadioGroup"
        android:id="@+id/p1_rg_company"
        android:layout_below="@id/textview3">

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg2_o1" />

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg2_o2" />

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg2_o3" />

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg2_o4" />
    </RadioGroup>

    <RadioGroup
        style="@style/AppRadioGroup"
        android:id="@+id/p1_rg_location">

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg3_o1" />

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg3_o2" />

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg3_o3" />

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg3_o4" />
    </RadioGroup>

Everything works ok, however I was a bit surprised to find out that when working with those via setOnCheckedChangeListener, the returned values is int from the total number of radio buttons.

Here's the code:

RadioGroup rgLocation = (RadioGroup) frg_view.findViewById(R.id.p1_rg_location);
    rgLocation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            Log.v(tag, Integer.toString(i)
        }
    });

So the first group returns integers from 1 to 4 and the second one from 5 to 8. My Application is API16

My expectation and necessity was that each group would have a separate "counter", so each group would have a value from 1 to 4 (which I need because of the data model).

Have I done something wrong and can it be fixed?

I couldn’t find any reference information from google regarding this behaviour

Was it helpful?

Solution

As noted, the int returned by onCheckedChanged is the id of the button:

public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (radioGroup.getCheckedRadioButtonId() == i){
            Log.v(TAG, "Equal" );
            }
        }

The workaround is quite lame in my opinion, but couldn't come up with better idea.

In the XML layout I define a numeric tag for each button in which order it is, example:

<RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg2_o3"
            android:tag="3"/>

        <RadioButton
            style="@style/AppRadioButtons"
            android:text="@string/p1_rg2_o4"
            android:tag="4"
            />

Then in the code:

public void onCheckedChanged(RadioGroup radioGroup, int i) {
         View v = getView().findViewById(i);
         int rbSelection = (int) v.getTag();
         dataObject.setPropertySelection(rbselection);
         }
    }

Seems a waste to call findviewbyid, to get a single int from tag, and this would complicate the code needed for populating the controls from the DB, as each tag would need to be mapped to an id.

but if someone has an idea how to improve this and just get which radio button is clicked by order in the radio group please do so.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top