Reputation: 45921
I'm developing an Android 3.1 tablet application and I need to create some Radio buttons programmatically:
RadioGroup radGrp = new RadioGroup(mActivity);
params.weight = 0.2f;
radGrp.setLayoutParams(params);
radGrp.setOrientation(RadioGroup.HORIZONTAL);
String tag = Long.toString(req.getRequirementId()) + "_" +
getString(R.string.yes);
RadioButton radioBtnYES = new RadioButton(mActivity);
radioBtnYES.setText(getString(R.string.yes));
radioBtnYES.setTag(tag);
radioBtnYES.setChecked(confValue);
radioBtnYES.setOnClickListener(radioListener);
radGrp.addView(radioBtnYES);
tag = Long.toString(req.getRequirementId()) + "_" +
getString(R.string.no);
RadioButton radioBtnNO = new RadioButton(mActivity);
radioBtnNO.setText(getString(R.string.no));
radioBtnNO.setTag(tag);
radioBtnNO.setChecked(!confValue);
radioBtnNO.setOnClickListener(radioListener);
radGrp.addView(radioBtnNO);
radGrp.setOnCheckedChangeListener(checkedChangedListener);
requirementLayout.addView(radGrp);
My problem is that when I tap on an unchecked radio button the other one on the same Radio Group it is still checked.
This is radioListener
code:
private RadioButton.OnClickListener radioListener =
new RadioButton.OnClickListener()
{
@Override
public void onClick(View v)
{
String tag = v.getTag().toString();
String[] parts = tag.split("_");
if ((parts != null) && (parts.length == 2))
{
boolean value = (parts[1].equals(getString(R.string.yes)));
Long id = Long.valueOf(parts[0]);
requirementsState.put(id, value);
}
}
};
And this is checkedChangedListener
code:
private RadioGroup.OnCheckedChangeListener checkedChangedListener =
new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton rb = (RadioButton)
mActivity.findViewById(checkedId);
group.setOnCheckedChangeListener(null);
group.clearCheck();
rb.setChecked(true);
group.setOnCheckedChangeListener(checkedChangedListener);
}
};
What am I doing wrong?
Upvotes: 2
Views: 646
Reputation: 45921
I've found the answer: **I need to setup checked radio button after add all radio butons to radio group.
This is my working code:
RadioGroup radGrp = new RadioGroup(mActivity);
params.weight = 0.2f;
radGrp.setLayoutParams(params);
radGrp.setOrientation(RadioGroup.HORIZONTAL);
String tag = Long.toString(req.getRequirementId()) + "_" +
getString(R.string.yes);
RadioButton radioBtnYES = new RadioButton(mActivity);
radioBtnYES.setText(getString(R.string.yes));
radioBtnYES.setTag(tag);
radioBtnYES.setOnClickListener(radioListener);
radGrp.addView(radioBtnYES);
tag = Long.toString(req.getRequirementId()) + "_" +
getString(R.string.no);
RadioButton radioBtnNO = new RadioButton(mActivity);
radioBtnNO.setText(getString(R.string.no));
radioBtnNO.setTag(tag);
radioBtnNO.setOnClickListener(radioListener);
radGrp.addView(radioBtnNO);
I've added two Radio Buttons to Radio Group and then:
if (confValue)
radioBtnYES.setChecked(true);
else
radioBtnNO.setChecked(true);
requirementLayout.addView(radGrp);
Upvotes: 1