Search code examples
androidradio-buttonradio-groupandroid-radiogroup

Strange behaviour of programatically created RadioGroup


I am programatically adding RadioButtons to a pre-existing but empty RadioGroup with the following code.

        RadioGroup currencySettingRadioGroup = (RadioGroup) currency_settings_dialog.findViewById(R.id.rg_currency_symbol);
        currencySettingRadioGroup.removeAllViews();

        RadioButton rb_none = new RadioButton(this);

        // Add the 'None' option at the start
        rb_none.setText("None");
        if (v_currency_symbol.equals("")) rb_none.setChecked(true);
        currencySettingRadioGroup.addView(rb_none,0);


        String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols);
        for ( int i=0; i < currency_symbols_options_array.length; i++ ) {
            RadioButton rb = new RadioButton(this);
            rb.setText(currency_symbols_options_array[i]);
            if (v_currency_symbol.equals(currency_symbols_options_array[i].substring(0,1))) rb.setChecked(true);
            currencySettingRadioGroup.addView(rb,i+1);
        }

The layout XML is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/currency_settings_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="12dp"
    android:paddingLeft="24dp"
    android:paddingRight="24dp"
    android:paddingTop="24dp">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/currency_symbol"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/currency_symbol_explanation" />

    <RadioGroup
        android:id="@+id/rg_currency_symbol"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    <Button
        android:id="@+id/settings_close_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:elevation="0dp"
        android:gravity="end|center_vertical"
        android:text="@string/close_currency_settings"
        android:textColor="#008dcd" />
</LinearLayout>

The RadioGroup gets built correctly and the RadioButton with the text that matches my v_currency_symbol variable's first character is checked as expected.

Correct RadioButton selected when dialog opens

However, clicking any of the other RadioButtons does not cause the checked option to uncheck - I end up with two options checked.

Clicking another option does not uncheck first

Clicking and checking any of the other options causes the second checked position to uncheck, but the first RadioButtonremains checked.

Other radio buttons behave like a separate radio group

It is almost as though the RadioButton that is checked programatically belongs to a seperate RadioGroup.

Removing the two lines that check one of the RadioButtons on creation allows the RadioGroup to function properly, but you obviously cannot then see the previous selection.


Solution

  • I found the issue... checking the RadioButton before adding it to the RadioGroup causes the problem.

    Swapping the two relevant lines resolves the issue. The working code is as follows:

        // Add the 'None' option at the start
        rb_none.setText("None");
        currencySettingRadioGroup.addView(rb_none,0);
        if (v_currency_symbol.equals("")) rb_none.setChecked(true);
    
    
        String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols);
        for ( int i=0; i < currency_symbols_options_array.length; i++ ) {
            RadioButton rb = new RadioButton(this);
            rb.setText(currency_symbols_options_array[i]);
            currencySettingRadioGroup.addView(rb,i+1);
            if (v_currency_symbol.equals(currency_symbols_options_array[i].substring(0,1))) rb.setChecked(true);
        }