-2

I am creating two radio buttons in a radio group programmatically.But both buttons are checked when i click on both.I mean not following the radio buttons property.I have two options YES and NO.And i need to get one checked at a time not both. my code is below...thanks in advance

public static void YesNoQuestion(Context context , View v,Question_Bean ques ,int ParentView){


    LinearLayout linearLayout = (LinearLayout) v.findViewById(ParentView);

    LayoutParams lp = new LayoutParams ( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    TextView question = new TextView(context);
    question.setText(ques.getQuestion());
    question.setTag(ques.getQid());


    RadioGroup rgroup = new RadioGroup(context);
    rgroup.setOrientation(RadioGroup.VERTICAL);
    RadioButton optionYes = new RadioButton(context);
    optionYes.setText("  Yes  ");
    RadioButton optionNo = new RadioButton(context);
    optionNo.setText("  No  ");

    rgroup.addView(optionYes,lp);
    rgroup.addView(optionNo,lp);
    linearLayout.addView(question,lp);
    linearLayout.addView(rgroup,lp);

}
SRB Bans
  • 3,096
  • 1
  • 10
  • 21

2 Answers2

3

it's Happening because by adding the radiobuttons programmatically you use the same id for both. You have to set different ids, or better: create 2 XML with 1 radiobutton in each with a different id and inflate them.

momor10
  • 478
  • 3
  • 11
0

Although answered I will also have to answer because of the @Opiatefuchs and @sourabhbans comments. As observed in the link I suggested:

optionYes.setId(100);
optionNo.setId(101);
msysmilu
  • 2,015
  • 23
  • 24