Radio Button setChecked/setSelected not working

11,109

Try this, before setting radio button to checked, clear all the checks in the radio group:

yourRadioGroup.clearCheck();
yourRadioButton.setChecked(true);
Share:
11,109
Gaurav
Author by

Gaurav

Updated on July 27, 2022

Comments

  • Gaurav
    Gaurav almost 2 years

    I have a list view with a text and 3 radio button. If i select any radio button, and scroll the list the radio button gets un-selected. I am maintaining the ID of items for which radio button is selected and in adapter Getview setting the required radio button to be selected. On debugging, it's executing statement setChecked(true) but the radio button is still unchecked. I tried setSelected(true) and setting through radio group ((RadioButton)holder.rg.getChildAt(2)).setChecked(true); but nothing seem to be working.

    XML:

    <TextView
            android:id="@+id/refill_product_name"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="@string/hello_world"
            android:textColor="@android:color/black"
            android:textStyle="normal"
            android:textSize="@dimen/item_sub_heading"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin"/>
    
        <RadioGroup
            android:id="@+id/refill_product_rg"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/refill_product_regular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/regular"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:buttonTint="@color/primary"/>
    
            <RadioButton
                android:id="@+id/refill_product_small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/small"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:layout_marginLeft="@dimen/radio_btn_margin"
                android:buttonTint="@color/primary"/>
    
            <RadioButton
                android:id="@+id/refill_product_extra_small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/extra_small"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:layout_marginLeft="@dimen/radio_btn_margin"
                android:buttonTint="@color/primary"/>
    
        </RadioGroup>
    

    Adapter Code:

    private class ViewHolder {
            TextView productName;
            RadioButton regularBtn, smallBtn, extraSmallBtn;
            RadioGroup rg;
        }
    

    GetView Method in Adapter

    holder.rg.clearCheck();
    if (mAppSession.getSelRefillProducts() != null) {
        for (RefillProduct pr : mAppSession.getSelRefillProducts()) {
             if (pr.getProductId() == rf.getProductId()) {
                if (pr.getSize().equals("R")) {
                   ((RadioButton)holder.rg.getChildAt(0)).setChecked(true);
                 } else if (pr.getSize().equals("S")) {
                    holder.smallBtn.setSelected(true);
                 } else if (pr.getSize().equals("XS")) {
                   ((RadioButton)holder.rg.getChildAt(2)).setChecked(true);
                 }
              }
             }
          }
    

    I am not sure if I am missing something, but setChecked or setSelected is not working. Please advise.

  • Gaurav
    Gaurav almost 8 years
    Thanks for the answer. I understand this behavior and I have maintained the list with item ID & corresponding option. In my GetView method, I am checking each item with my list and then setting the radio button as checked or not. On debugging, its executing the statement setChecked(true) but still not working.
  • Vidya Sagar
    Vidya Sagar almost 4 years
    I don't know why it this to be done like this but this answered saved me after 3 hours of debugging
  • JPI93
    JPI93 over 2 years
    Absolute life saver!