How to set only one RadioButton Can be selected at the time in RadioGroup

梦想的初衷 提交于 2019-11-30 11:31:44

It's not working because of TableRow inside RadioGroup. All RadioButtons are not grouped together because of TableRow between them.

RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.

Just change your code like this it will work :

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RGroup">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Abdominal"
                android:id="@+id/Abdominal"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Arm"
                android:id="@+id/Arm"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back"
                android:id="@+id/Back" />                                        

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Chest"
                android:id="@+id/Chest"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Leg"
                android:id="@+id/Leg"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Shoulder"
                android:id="@+id/Shoulder"/>

        </RadioGroup>

Hope this helps. :)

  <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:id="@+id/radioGroup">
        <RadioButton
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="wrap_content"
            android:text="Debit"
            android:id="@+id/rDebit"
            android:checked="false"
             />

        <RadioButton
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="wrap_content"
            android:text="Credit"
            android:id="@+id/rCredit"
            android:checked="false" />

    </RadioGroup>

And in java file

 RadioGroup radioGroup;



radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

And when to do something

 if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}

Simple way. onclick of radio button. do code as per below.

public void clearRadioChecked() {
    rdopa.setChecked(false);
    rdopb.setChecked(false);
    rdopc.setChecked(false);
    rdopd.setChecked(false);
}

if you wann select rdopa then on click of rdopa do as below.

clearRadioChecked()
rdopa.setChecked(true);

You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.

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

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="50dp"
            android:text="Yes" />

        <RadioButton
            android:id="@+id/rbNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No" />
</RadioGroup>

You can create your own RadioGroup, that is able to find all RadioButtons that are nested in your group regardless of direct child or not. Below the code of my NestedRadioGroup. Using this instead of your RadioGroup in your xml file should do the trick.

public class NestedRadioGroup extends LinearLayout {
    private SparseArray<RadioButton> radioButtons;
    private int checkedId;

    public NestedRadioGroup(Context context) {
        this(context, null);
    }

    public NestedRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        radioButtons = new SparseArray<>();
        checkedId = -1;
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        findRadioButtons(child);
    }

    private void findRadioButtons(View child) {
        if (child instanceof RadioButton) {
            RadioButton newButton = (RadioButton) child;
            newButton.setId(radioButtons.size());
            newButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (isChecked) {
                    if (checkedId != -1) {
                        radioButtons.get(checkedId).setChecked(false);
                    }
                    radioButtons.get(buttonView.getId()).setChecked(true);
                    checkedId = buttonView.getId();
                }
            });
            radioButtons.put(newButton.getId(), newButton);
        } else if (child instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) child;

            for (int i = 0; i < group.getChildCount(); i++) {
                this.findRadioButtons(group.getChildAt(i));
            }
        }
    }
}

Any ideas to make the code cleaner?

I hope it works out for you :)

Vivek Thummar

I faced same problem just right now and found where I was making mistakes and I describes it below:

  1. If you are using RadioButtons in RadioGroup then RadioButtons should be direct child of RadioGroup.

  2. I check my first condition and it was correct but still all radio buttons are getting selected and when gives ids and run project, it automatically solved my problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!