Check the box by default.

I create a RadioGroup with RadioButton dynamically and I need to set one radio button by default.

I did this using both radioButton.setChecked(true) and radioButton.toggle();

The problem is that when I select another radio button at runtime, the first one is checked, so I get two checked radio buttons in the radio group.

Has anyone had this problem and knew 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; } 
+6
source share
7 answers

I know I'm late, but for those looking for an answer like me, this can be useful.

When you add a RadioButton to a RadioGroup, you must check the box by adding it to the parent element, for example:

 RadioGroup radioGroup = new RadioGroup(getContext()) RadioButton radioButton = new RadioButton(getContext()); radioButton.setText("text"); mRadioGroup.addView(radioButton); radioButton.setChecked(true); 

If the check box is selected before adding it to the parent element, it cannot be unchecked.

+7
source

Simple, load the screen for the first time, check the position of the In Getview method, and then apply the condition:

 if (position == 0) { holder.act_delivery_rbtn.setChecked(true); } 
+2
source

If you use only one switch to test and turn off, perhaps you should use a checkbox or switch button.

http://developer.android.com/resources/tutorials/views/hello-formstuff.html

Scroll down and see the checkbox and the toggle button.

When using radio stations, you usually have more than one and choose between them. Like light, medium, hard.

Replace

 radio.toggle(); 

with

 radio.setChecked(true); 
+1
source

You can do it 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> 
+1
source

you have to check it in the radio group ...

 radiogroup.check(IdOfYourButton) 
0
source

use the clearCheck () function to clear the already marked buttons. I hope this solves your problem.

0
source

It turned out that I called the checked () method in the radio group right after setting OnCheckedChangeListener (), calling it immediately and throwing NPE for some reason.

The call to the checked () method has been moved just before OnCheckedChangeListener (), and now it works.

 radios.check(2); radios.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { } }); 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.setId(2); return radio; } 
0
source

All Articles