Home » Android » radio button – android radiobutton check after clearing check issue

radio button – android radiobutton check after clearing check issue

Posted by: admin June 15, 2020 Leave a comment

Questions:

friends. I’m having a really silly problem with radiogroup. Yet I’m unable to find solution.

I will try to describe how to reproduce my problem:
I have radiobutton group and two buttons inside.
I select one of them, lets say 1st one.
Then I’m clearing selection by calling radioGroup.clearCheck()
After I’m trying to select 1st button, but its not checking.
If I check 2nd, it checks normally.
If I check 1st after checking 2nd it also works normally.

This may sound crazy, yet I can’t fix it. Please help me, thanks in advance.

I use

@Override
protected void init() {
    View view = View
            .inflate(getContext(), R.layout.wo_task_yn_result, null);

    performed = (RadioButton) view.findViewById(R.id.yn_yes);
    notPerformed = (RadioButton) view.findViewById(R.id.yn_no);

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

    performed.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(final CompoundButton buttonView,
                final boolean isChecked) {
            Log.d(YES, "verify");
            if (isChecked) {
                Log.d(YES, "checked");
                result = YES;
            }
        }
    });

    notPerformed.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(final CompoundButton buttonView,
                final boolean isChecked) {
            Log.d(NO, "verify");
            if (isChecked) {
                Log.d(NO, "checked");
                result = NO;
            }
        }
    });

    addView(view);
}

To create buttons and

@Override
public void clear() {
    radioGroup.clearCheck();

    result = "";
}

for clearing them

How to&Answers:

I had the same problem. Was unable to select the first radioButton in the group, but could select the other two. And after selecting the second radioButton, I could select the first one as well. I resolved it by doing a single radioGroup.clearCheck() instead of individual radioButtonA.setChecked(false)

Answer:

Use OnCheckedChangeListener and make use of the method setSelected(true) or setChecked(true).

Answer:

if u want to uncheck radio button try this method:

@Override
public void clear() {

performed.setChecked(false);

nonperformed.setChecked(false);

}