I have some problem with radio group, I am new to Android development so I don\'t have much idea to how can handle it.This is form which has some input filed and radio group
RadioGroup yourRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
yourRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.radio0:
// TODO Something
break;
case R.id.radio1:
// TODO Something
break;
case R.id.radio2:
// TODO Something
break;
}
}
});
Or, if you prefer:
yourRadioGroupName.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
int idRadioButtonChosen = yourRadioGroupName.getCheckedRadioButtonId();
if(idRadioButtonChosen > 0){
radioButtonChosen = (RadioButton) findViewById(idRadioButtonChosen);
textViewOnclick.setText(radioButtonChosen.getText());
}
}
});
The thing is that your radio buttons trying to refer the onRadioButtonClicked
method in your main activity. But you didnt specify that method. Remember, if you're using android:onClick
you should not use setOnCheckedChangeListener
in your Main activity and vice versa.
In kotlin You can use bellow like..
radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
val radio:RadioButton = group.findViewById(checkedId)
Log.e("selectedtext-->",radio.text.toString())
})
Or
radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
var selectedId = rg.checkedRadioButtonId
val radio:RadioButton = group.findViewById(selectedId)
Log.e("selectedtext-->",radio.text.toString())
})
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb=(RadioButton)findViewById(checkedId);
Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
}
});