1. Declare variables of radio group and button-
RadioButton radioButton_hot, radioButton_warm, radioButton_cold;
RadioGroup radioGroup;
RadioButton radioButton;
String mSelectedRadioButton=“”;
radioGroup = findViewById(R.id.radioGroup);
radioButton_hot = findViewById(R.id.radioButton_hot);
radioButton_warm = findViewById(R.id.radioButton_warm);
radioButton_cold = findViewById(R.id.radioButton_cold);
2. Set event in radio group -
radioGroup.setOnCheckedChangeListener(this);
3. Implement checked change event listener method-
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int selectedId;
switch (checkedId) {
case R.id.radioButton_hot:
selectedId = group.getCheckedRadioButtonId();
radioButton_hot = findViewById(selectedId);
mSelectedRadioButton = radioButton_hot.getText().toString();
break;
case R.id.radioButton_warm:
selectedId = group.getCheckedRadioButtonId();
radioButton_warm = findViewById(selectedId);
mSelectedRadioButton = radioButton_warm.getText().toString();
break;
case R.id.radioButton_cold:
selectedId = group.getCheckedRadioButtonId();
radioButton_cold = findViewById(selectedId);
mSelectedRadioButton = radioButton_cold.getText().toString();
break;
}
}
4. Get the selected radio button text in mSelectedRadioButton variable.
This code is really helpful .........
ReplyDeleteSurya Informatics
Thanks for sharing relevant solution.
ReplyDelete