RadioGroup not working in fragment android
1. Try to call method configViews()
from onCreateView()
and pass the view kf06_view
as parameter.
2. Implement View.OnClickListener
to handle button_kf06_back
and button_kf06_next
click events.
Update your K06_Away
Fragment as below:
public class K06_Away extends Fragment implements View.OnClickListener {
protected Typeface tfLatoBold,tfLatoMedium,tfLatoRegular;
private Button button_kf06_back,button_kf06_next;
private TextView txtVw_kf06_resident_away;
private EditText edTxt_kf06_visiting_lastname;
private RadioGroup radioGp_kf06_resident_away;
List<RadioButton> radioButtons = new ArrayList<RadioButton>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View kf06_view = inflater.inflate(R.layout.k06_away, container, false);
// Init
configViews(kf06_view);
return kf06_view;
}
private void configViews(View view) {
button_kf06_back = (Button) view.findViewById(R.id.button_kf06_back);
button_kf06_next = (Button) view.findViewById(R.id.button_kf06_next);
txtVw_kf06_resident_away= (TextView) view.findViewById(R.id.txtVw_kf06_resident_away);
radioGp_kf06_resident_away = (RadioGroup) view.findViewById(R.id.radioGp_kf06_resident_away);
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_1_2_hours) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_halfday) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_allday) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_moreday) );
configClickListeners();
radioButtonAction();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_kf06_back:
// Do something...
break;
case R.id.button_kf06_next:
// Do something...
break;
}
}
private void configClickListeners() {
button_kf06_back.setOnClickListener(this);
button_kf06_next.setOnClickListener(this);
}
private void radioButtonAction(){
for (RadioButton button : radioButtons){
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) processRadioButtonClick(buttonView);
String radio_Text = buttonView.getText().toString();
int radio_Id = buttonView.getId();
System.out.println("Selected the Radio:"+radio_Text+", Radio-Id:"+radio_Id);
}
});
}
}
private void processRadioButtonClick(CompoundButton buttonView){
for (RadioButton button : radioButtons){
if (button != buttonView ) button.setChecked(false);
}
}
}
RadioGroup Directly contain only RadioButtons,No other Layouts,if you add LinearLayout inside RadioGroup,it will not works as you expect.Only use RadioButtons as child of RadioGroup.