Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: RadioGroup - How to configure the event listener

From my understanding, to determine if a checkbox is "clicked" and find if it's checked or not, code such as the following can be used:

cb=(CheckBox)findViewById(R.id.chkBox1);         cb.setOnCheckedChangeListener(this);  public void onCheckedChanged(CompoundButton buttonView,      boolean isChecked) {          if (isChecked) {              cb.setText("This checkbox is: checked");          }          else {              cb.setText("This checkbox is: unchecked");          }      } 

However, I am unable to work out the logic on how to do the above for a radiogroup.

Here is the xml for my RadioGroup:

<RadioGroup android:id="@+id/radioGroup1"  android:layout_width="wrap_content"  android:layout_height="wrap_content">     <RadioButton android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/radio1" android:checked="true"      android:text="RadioButton1">     </RadioButton>     <RadioButton android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/radio2" android:text="RadioButton2" android:checked="true">     </RadioButton>     <RadioButton android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/radio3" android:text="RadioButton3">     </RadioButton> </RadioGroup> 

Question: Do i need to setup another listener, or will the listener already there also "register" this group?

Also, should the listener be set up on the RadioGroup or the RadioButton?

like image 796
Ryan Avatar asked Jul 21 '11 18:07

Ryan


People also ask

How to add RadioButton in android?

Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.

How to handle Radio button in android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.

How do you check whether a RadioButton is selected or not in android?

Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected.

How do I get radio buttons side by side on android?

Use the android:orientation="horizontal" -attribute of the RadioGroup.


1 Answers

This is how you get the checked radiobutton:

// This will get the radiogroup RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1); // This will get the radiobutton in the radiogroup that is checked RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId()); 

To use the listener, you do this:

// This overrides the radiogroup onCheckListener rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {     public void onCheckedChanged(RadioGroup group, int checkedId)     {         // This will get the radiobutton that has changed in its check state         RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);         // This puts the value (true/false) into the variable         boolean isChecked = checkedRadioButton.isChecked();         // If the radiobutton that has changed in check state is now checked...         if (isChecked)         {             // Changes the textview's text to "Checked: example radiobutton text"             tv.setText("Checked:" + checkedRadioButton.getText());         }     } }); 
like image 165
A. Abiri Avatar answered Oct 04 '22 02:10

A. Abiri