This is a follow up question to this question:
Android: How to get a radiogroup with togglebuttons?
I am also trying to implement a radio group with toggle buttons and I have followed the answer to the above question. It says to create a radio group with toggle buttons and then set onCheckChangeListener
but it won't trigger.
This is the layout:
<RadioGroup
android:id="@+id/howWellSleptRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ToggleButton
android:id="@+id/imageButtonSleptWell1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:minWidth="0dp"
android:minHeight="0dp"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/selector_well_sleep_1"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<ToggleButton
android:id="@+id/imageButtonSleptWell2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:minWidth="0dp"
android:minHeight="0dp"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/selector_well_sleep_2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<ToggleButton
android:id="@+id/imageButtonSleptWell3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:minWidth="0dp"
android:minHeight="0dp"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/selector_well_sleep_3" />
</RadioGroup>
And this is the listener and its attachment:
private final RadioGroup.OnCheckedChangeListener radioGroupToggleListener = new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(final RadioGroup radioGroup, final int buttonId)
{
for (int i = 0; i < radioGroup.getChildCount(); i++)
{
View v = radioGroup.getChildAt(i);
if(v instanceof ToggleButton)
{
final ToggleButton view = (ToggleButton)v;
view.setChecked(view.getId() == buttonId);
}
}
}
};
in onCreateView
RadioGroup wellSleptRg = (RadioGroup)v.findViewById(R.id.howWellSleptRadioGroup);
wellSleptRg.setOnCheckedChangeListener(radioGroupToggleListener);
Why won't the listener trigger?