Search code examples
androidoncheckedchangedandroid-radiogroup

All radio group listeners called on click of any one radio button


    RadioGroup r1 = (RadioGroup) view.findViewById(R.id.first);
    r1.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) 
        {
            Utilities.createDebugToastMsg(rootView.getContext(), "clicked on R1");
        }
    });

    RadioGroup r2 = (RadioGroup) view.findViewById(R.id.second);
    r2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Utilities.createDebugToastMsg(rootView.getContext(), "clicked on R2");
        }
    });
}

I have TWO separate radio groups, consisting of THREE radio buttons each. Now which ever radio button I click, both the listeners are called and I get both the toast messages. Please help and point out if there is anything I have missed or messed up. Thanks is advance.


Solution

  • Please have a switch case in your code. Like below:

         switch (view.getId()) {
    
                    case R.id.first:
                        your code here for what to do when it is clicked/checked
                        break;          
                        case R.id.second:
                        your code here for what to do when it is clicked/checked
                        break; 
    }
    

    As you don't have any switch case in your code, both the functions are executing. So, do the above implementation which will help you to do what you require...