Set default checked Radio Button

26,553

Solution 1

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.

Solution 2

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>

Solution 3

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);
}

Solution 4

Just make checked = "true" in your code

Solution 5

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);
Share:
26,553
thehindutimes
Author by

thehindutimes

Updated on July 05, 2022

Comments

  • thehindutimes
    thehindutimes almost 2 years

    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;
        }
    
  • thehindutimes
    thehindutimes over 10 years
    I tried both and they both check the button but the button still stays checked if I check another one
  • SilentKiller
    SilentKiller over 10 years
    are you adding them into single RadioGroup.?