Reputation: 49097
I am trying to create a small list (3 rows) where there are one RadioButton
and one EditText
view on each row arranged horizontally. I want the radiobuttons to be in the same RadioGroup
. I can't add the radio button and the edit text view to a linearlayout because then Android yells about the radiobutton having another parent. How can I solve this?
I am adding each row programatically:
mAnswerGroup.addView(radioButton, new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mAnswerGroup.addView(editText, new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Upvotes: 1
Views: 503
Reputation: 5916
In this case, a RadioGroup
won't work. You will have to manually handle the checked changes of the 3 RadioButtons
because you need to make a LinearLayout and add a RadioButton
and EditText
inside it. You will have to manually uncheck the other radio buttons.
Set the onCheckedChangeListener of each RadioButton
and handle then manually:
Implement OnCheckedChangeListener
interface in your activity and do this in the onCreate()
:
rb_1.setOnCheckedChangeListener(this);
rb_2.setOnCheckedChangeListener(this);
rb_3.setOnCheckedChangeListener(this);
Then, override the onCheckedChanged()
method of the interface:
@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if(isChecked){
//get the id of the checked button for later reference
int id = cb.getId();
/*
* do what you want here based on the id you got
*/
//uncheck the other RadioButtons
rb_1.setChecked(id == R.id.rb_1);
rb_2.setChecked(id == R.id.rb_2);
rb_3.setChecked(id == R.id.rb_3);
}
}
Upvotes: 4