I have a RadioGroup in my activity that contains four ToggleButtons with a custom background. I want the user to be able to choose any one of the buttons at once, and there should never be no button selected, but even though it's in a RadioGroup and all else works fine, selecting the already-selected ToggleButton unselects it, leaving no button selected.
How can I force a ToggleButton to stay selected if the user taps it a second time?
My XML:
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" />
Relevant chunk of my onCreate():
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(onRadioGroupClickListener);
for (int index = 0; index < OPTIONS.length; index++) {
ToggleButton button = new ToggleButton(this);
button.setId(index);
button.setText(OPTIONS[index]);
button.setTextOn(OPTION[index]);
button.setTextOff(OPTIONS[index]);
button.setChecked(index == 0); // Set to first option by default
button.setButtonDrawable(Color.TRANSPARENT);
button.setBackgroundResource(R.drawable.selector);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
RadioGroup parent = (RadioGroup) view.getParent();
ToggleButton button = (ToggleButton) view;
TimesheetLog.d("View is checked: " + button.isChecked());
parent.check(button.getId());
currentSelection = view.getId();
}
});
radioGroup.addView(button);
}
If I need to add any more code, tell me. Thanks!