Set default checked Radio Button

Viewed 27060

I'm creating a RadioGroup with RadioButtons dynamically and need to have one radio button checked by default.

I've done this by using both radioButton.setChecked(true) and radioButton.toggle();

The problem I have is that when I at runtime select another radio button the first one stays checked so I end up with two checked radio buttons in the radio group.

Has anyone had this problem and know how to solve it?

private RadioButton addRadioButton(String type, String price){
        RadioButton radio = new RadioButton(Order.this);
        radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
        radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
        radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        radio.setTag(price);

        if(type.toLowerCase().equals("m"))
            radio.toggle();

        return radio;
    }
9 Answers

Just make checked = "true" in your code

in Xml add android:checkedButton="@+id/storeRB" to RadioGroup

 <RadioGroup
                android:id="@+id/storeTypeRG"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checkedButton="@+id/storeRB"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="@+id/text2"
                app:layout_constraintStart_toEndOf="@+id/text2"
                app:layout_constraintTop_toTopOf="@+id/text2">

                <RadioButton
                    android:id="@+id/storeRB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="24dp"
                    android:button="@drawable/radio_button_bg"
                    android:paddingStart="8dp"
                    android:text="@string/store_str"
                    app:layout_constraintBottom_toBottomOf="@+id/text2"
                    app:layout_constraintStart_toEndOf="@+id/text2"
                    app:layout_constraintTop_toTopOf="@+id/text2" />

                <RadioButton
                    android:id="@+id/hyperRB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:button="@drawable/radio_button_bg"
                    android:paddingStart="8dp"
                    android:text="@string/hyper_str"
                    app:layout_constraintBottom_toBottomOf="@+id/text2"
                    app:layout_constraintStart_toEndOf="@+id/storeRB"
                    app:layout_constraintTop_toTopOf="@+id/text2" />

                <RadioButton
                    android:id="@+id/restaurantRB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:button="@drawable/radio_button_bg"
                    android:paddingStart="8dp"
                    android:text="@string/restaurant_str"
                    app:layout_constraintBottom_toBottomOf="@+id/text2"
                    app:layout_constraintStart_toEndOf="@+id/hyperRB"
                    app:layout_constraintTop_toTopOf="@+id/text2" />
            </RadioGroup>

enter image description here

Related