Android: RadioGroup - How to configure the event listener

107,050

Solution 1

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());
        }
    }
});

Solution 2

It should be something like this.

RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {

            }
        }

    });

Based on the checkedId, you would know which of the radiobutton has been clicked and then use your above code to figure out if its checked or unchecked. This is homework. ;)

Solution 3

Using Switch is better:

radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      public void onCheckedChanged(RadioGroup arg0, int id) {
        switch (id) {
        case -1:
          Log.v(TAG, "Choices cleared!");
          break;
        case R.id.chRBtn:
          Log.v(TAG, "Chose Chicken");
          break;
        case R.id.fishRBtn:
          Log.v(TAG, "Chose Fish");
          break;
        case R.id.stkRBtn:
          Log.v(TAG, "Chose Steak");
          break;
        default:
          Log.v(TAG, "Huh?");
          break;
        }
      }
    });

Solution 4

//Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_pirates:
        if (checked)
            // Pirates are the best
        break;
    case R.id.radio_ninjas:                       
        if (checked)
            // Ninjas rule
        break;
}
}
Share:
107,050
Ryan
Author by

Ryan

Updated on July 09, 2022

Comments

  • Ryan
    Ryan almost 2 years

    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?