3

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!

Cornholio
  • 985
  • 1
  • 5
  • 22
  • 1
    Um, why are you not using radio buttons in your radio group? They behave the way you want – David Wasser Apr 26 '13 at 12:04
  • I had a lot of trouble getting anything to work, and as far as I know, radio buttons don't ever really look like real buttons. Am I wrong about that? I thought that the text was always off to the side... – Cornholio Apr 26 '13 at 12:25
  • They look like radio buttons :-) Yes, the text is off to the side. Did you look at [this question](http://stackoverflow.com/questions/2379527/android-how-to-get-a-radiogroup-with-togglebuttons) ? – David Wasser Apr 26 '13 at 12:39
  • Yep, that's the answer I based my code on - The only problem with it is that you can have none selected, but I want to keep at least one selected at all times. btw, thanks for the response! – Cornholio Apr 26 '13 at 12:51

2 Answers2

1

The line below should be in the same class with toggle click event;

private RadioButton mBtnCurrentRadio;

Toggle click event (should be in the activity);

public void onToggle(View view) {

        final ToggleButton mBtnToggle = (ToggleButton) view;

        // select only one toggle button at any given time
        if (mBtnCurrentToggle != null) {
            mBtnCurrentToggle.setChecked(false);
        }
        mBtnToggle.setChecked(true);
        mBtnCurrentToggle = mBtnToggle;

        // app specific stuff ..
    }

RadioGroup xml code for layout;

<RadioGroup
        android:id="@+id/toggleGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:orientation="horizontal" >

        <ToggleButton
            android:id="@+id/btn_Letter"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onToggle"
            android:textOff="Letter"
            android:textOn="Letter"
            style="@style/toggleBtnStyle" />

        <ToggleButton
            android:id="@+id/btn_A4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onToggle"
            android:textOff="A4"
            android:textOn="A4"
            style="@style/toggleBtnStyle" />

        <ToggleButton
            android:id="@+id/btn_A3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onToggle"
            android:textOff="A3"
            android:textOn="A3"
            style="@style/toggleBtnStyle" />
    </RadioGroup>
séan35
  • 966
  • 2
  • 13
  • 37
0

I had the same question, and just figured it out.

Using the code snippet you have in your question, if you add one line:

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.clearCheck(); // <----------- ADD THIS-------
        // -------------------------------------------------
        parent.check(button.getId());
        currentSelection = view.getId();
    }
});

I believe it should work. What I think is happening is the check() method is actually a toggle in its own. If we clear the check every time, then every time we call check() afterwards it will guarantee that it is in the pressed state. This makes it impossible to uncheck a ToggleButton.

boltup_im_coding
  • 6,345
  • 6
  • 40
  • 52