Search code examples
androidandroid-buttonandroid-radiogroupandroid-radiobutton

RadioButton Issue get checked when sending?


Having two RadioButtons and button send.
When I click on the send button the 2 RadioButtons are checked.

How to avoid this? enter image description here


Solution

  • You need to group the radio buttons so inside the xml add the radio buttons inside this tag:

    <RadioGroup> .....</RadioGroup>
    

    then in your activity do this:

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup); //declare the radiogroup
    
    
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
          @Override
    
            public void onCheckedChanged(RadioGroup group, int checkedId) {
    
                // find which radio button is selected
    
                if(checkedId == R.id.radiobtn1) {
    
                    //code here
                } else if(checkedId == R.id.radiobtn2) {
    
                    //code here
    
                } else {
    
                   //code here
                }
            }
        });