0

There is a question having 4 options(Radio buttons). I want to add the option of selected radio button into an array. ONLY the option of the selected radio button. The Radio Group is having 4 radio buttons.

Shagun
  • 7
  • 4

1 Answers1

0

You should wrap all the radiobuttons in a RadioGroup and then set its OnCheckedChangeListener:

final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            View radioButton = group.findViewById(checkedId);
            // add radioButton to Your array
        }
    });

Note that this code will add to the array the selected radio button every time it gets selected. If you want to add it on a button click, you need to do it this way:

final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int checkedId = radioGroup.getCheckedRadioButtonId();
        View radioButton = radioGroup.findViewById(checkedId);
        // add radioButton to Your array
    }
})
gianlucaparadise
  • 1,678
  • 20
  • 32