Set default checked Radio Button

28k views Asked by At

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

There are 9 answers

2
SilentKiller On

If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.

http://developer.android.com/resources/tutorials/views/hello-formstuff.html

Scroll down and see checkbox and toggle button.

When using radios you usually have more than one and choose between them. Like easy, medium, hard.

Replace

radio.toggle();

with

radio.setChecked(true);
0
nitesh goel On

you should check it in radio group...

radiogroup.check(IdOfYourButton)
0
Yogamurthy On

use the clearCheck () function to clear the already checked buttons.I hope this solves your problem.

0
thehindutimes On

Turned out to be because I called the checked() method on the radio group straight after setting the OnCheckedChangeListener() causing it to be called immediately and throw a NPE for some reason.

Moved the checked() method call to just before the OnCheckedChangeListener() and it works now.

radios.check(2);
radios.setOnCheckedChangeListener(new OnCheckedChangeListener() {   
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {

                }
            });

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.setId(2);

        return radio;
    }
0
Sergio Carvalho On

I know I'm late but for those who search for the answer like me this can be useful.

When you add RadioButton to a RadioGroup, you must set the button checked after adding it to the parent like:

RadioGroup radioGroup = new RadioGroup(getContext())
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText("text");
mRadioGroup.addView(radioButton);
radioButton.setChecked(true);

If the view is checked before adding it to the parent, it will be impossible to uncheck it.

0
dorgelesn On

You can do this simply:

JAVA:

    radiogroup =(RadioGroup)findViewById(R.id.radiogroup);
    radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
    radiobutton2 =(RadioButton)findViewById(R.id.radiobutton2);

    if(your first assessment){
        radiobutton1.setChecked(true);
    }else{
        radiobutton2.setChecked(true);
    }

XML:

   <RadioGroup
        android:id="@+id/radiogroup"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
        android:id="@+id/radiobutton1"
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <RadioButton
        android:id="@+id/radiobutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    </RadioGroup>
0
Maulik Santoki On

Simple, at first time load the screen check the position of In Getview method and then apply condition:

if (position == 0) {
  holder.act_delivery_rbtn.setChecked(true);
}
0
Dostonbek Oripjonov On

Just make checked = "true" in your code

0
Mohamed AbdelraZek On

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