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.
Asked
Active
Viewed 629 times
0
-
do you use RadioGroup or only RadioButtons ? – Lubomir Babev Jul 18 '16 at 10:05
-
take radiogroup of four buttons. and from the object of radiogroup you will get the selected radio button. – Jul 18 '16 at 10:05
-
There is a radiogroup having 4 radio buttons. Specify the code. – Shagun Jul 18 '16 at 10:07
-
then check this answer : http://stackoverflow.com/a/18179176/5519005 – Lubomir Babev Jul 18 '16 at 10:10
-
I want to add the selected radio button in an array – Shagun Jul 18 '16 at 10:17
1 Answers
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