halxinate
halxinate

Reputation: 1559

Styled Android RadioButtons in RadioGroup not working

I have simple layout containing a Scroll View, like this:

<ScrollView
        android:id="@+id/inputfields"
        android:layout_width="fill_parent"
        android:layout_height="280dp"
        android:clipChildren="true"
        android:measureAllChildren="true"
        android:visibility="gone" >
</ScrollView>

The rest of the layout doesn't matter.

In the OnCreate() I'm adding my RadioButtons to the new RadioGroup, assigning my actions by setOnCheckedChangeListener(), and adding the RadioGroup to the ScrollView container. See the code excerpt below.

protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.inputdlg);
//Populate the list
ScrollView layout = ScrollView)findViewById(R.id.inputfields);

Context cont = getContext();

final RadioGroup rg = new RadioGroup(cont);
rg.setScrollContainer(true);

int id = 0; //For the radiogroup the id does not need to be unique
for(String s : mStringsList) {
    RadioButton rb = new RadioButton(cont);
    rb.setText(s);
    rb.setId(id++);
    rb.setGravity(Gravity.CENTER_VERTICAL);
    rg.addView(rb);
}


rg.check(Integer.getInteger(mValue,0)); //0 is the default
rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(RadioGroup group,  int checkedId) {
        if(mRadioListener!=null)
            mRadioListener.onClick(mEt.getText().toString());
        dismiss();
    }
});
layout.addView(rg);
}

Problem. The view scrolls well, but the radio buttons are not working at all. What I'm missing?

Upvotes: 2

Views: 3454

Answers (1)

halxinate
halxinate

Reputation: 1559

It was not the ScrollView or anything, but the custom RadioButton Style I have defined inproperly in the Styles.XML

See the difference below:

Bad one:

<style name="Widget.RadioButtonR" parent="android:Widget">
    <item name="android:background">@drawable/btn_radio_label_background</item>
    <item name="android:button">@drawable/btn_radio_r</item>
</style>

Right one:

<style name="Widget.RadioButtonR" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:background">@drawable/btn_radio_label_background</item>
    <item name="android:button">@drawable/btn_radio_r</item>
    <item name="android:textColor">@color/myfontcolor</item>
</style>

Upvotes: 2

Related Questions