Skip to content Skip to sidebar Skip to footer

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_settin

Solution 1:

I found the issue... checking the RadioButtonbefore 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);
    }

Post a Comment for "Strange Behaviour Of Programatically Created Radiogroup"