-2

How I reset a radio button inside a radio group like the image example below

All my radio groups and its radio buttons are created programmatically.

I tried to to set OnClickListener to get the radio button value before getting changed but, it didn't help.

enter image description here

Edit: I posted the answer below, please check it.

Hadi Al Tinawi
  • 429
  • 7
  • 22

3 Answers3

0

You have to uncheck one radiobutton while checking the other.

Simple way.

public void clearRadioChecked() {
 alt1.setChecked(false);
 blt2.setChecked(false);

  }

if you want to select alt1 then on click of alt1 do as below.

 clearRadioChecked()
 alt1.setChecked(true);
Phillen
  • 336
  • 5
  • 18
0

try this :

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.radioButton){
                 //do something     
                }
                else if(checkedId == R.id.radioButton2){

                }
                else{

                }
        });

xml layout

<RadioGroup
      android:layout_width="fill_parent"
      android:layout_height="90dp"
      android:layout_below="@+id/imageView"
      android:layout_marginTop="58dp"
      android:weightSum="1"
      android:id="@+id/radioGroup"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_alignRight="@+id/textView3"
      android:layout_alignEnd="@+id/textView3">

      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="55dp"
         android:text="Male"
         android:id="@+id/radioButton"
         android:layout_gravity="center_horizontal"
         android:checked="false"
         android:textSize="25dp" />

      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Female"
         android:id="@+id/radioButton2"
         android:layout_gravity="center_horizontal"
         android:checked="false"
         android:textSize="25dp"
         android:layout_weight="0.13" />
   </RadioGroup>
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
  • I tried to set OnCheckedChangeListener but I am always getting the Ischecked = true. How I can know if this radio button has been checked before so I can clear it? – Hadi Al Tinawi Apr 28 '17 at 11:32
  • Glad it worked. You can also visit : http://stackoverflow.com/tour to get more information about SO and support me as much as you can. – Bhupat Bheda Apr 28 '17 at 12:21
0

This is how I did it:

It is in C# but I think it is easy to convert it to Java.

I used tag to know if this radio button checked before or not.

False => It is not checked

True => It is checked

radiobutton.Tag = false;

radiobutton.Click += SingleChoiceQuestionAlternativeClick;

Then:

private void SingleChoiceQuestionAlternativeClick(object sender, EventArgs e)
    {
        RadioButton questionAlternative = sender as RadioButton;

        if (questionAlternative != null)
        {
            RadioGroup questionAlternatives = questionAlternative.Parent as RadioGroup;

            if (questionAlternatives != null)
            {
                if (questionAlternative.Tag.Equals(false))
                {
                    for (int i = 0; i < questionAlternatives.ChildCount; i++)
                    {
                        RadioButton childRadioButton = questionAlternatives.GetChildAt(i) as RadioButton;

                        if (childRadioButton != null)
                        {
                            childRadioButton.Tag = false;
                        }
                    }

                    questionAlternative.Tag = true;
                }
                else
                {
                    questionAlternative.Tag = false;
                    questionAlternatives.ClearCheck();
                }
            }
        }
    }
Hadi Al Tinawi
  • 429
  • 7
  • 22