MultiRowsRadioGroup
MultiRowsRadioGroup copied to clipboard
How to add these button programatically?
Hi, Can you tell me how i add these control by code. it is working using xml but not from.
Thanks.
Use like RadioGroup:
MultiRowsRadioGroup group = new MultiRowsRadioGroup(context);
...
group.addView(child);
...
I have tried this already but it checks all the Radio buttons this is my code.
MultiRowsRadioGroup rg = Constants.CreateRadioGroup(CampaignFormData.this, questionDetail);
LinearLayout buttonsLayout = new LinearLayout(CampaignFormData.this);
buttonsLayout.setLayoutParams(params);
buttonsLayout.setPadding(10, 5, 10, 5);
buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonsLayout.setWeightSum(2);
rg.addView(buttonsLayout);
for (int choices = 0; choices < questionDetail.getQuestionChoices().size(); choices++) {
RadioButton radioButton = Constants.CreateRadioButton(CampaignFormData.this, questionDetail.getQuestionChoices().get(choices), questionDetail.getQuestionChoices().size());
if (choices % 2 == 0 && choices != 0) {
buttonsLayout = new LinearLayout(CampaignFormData.this);
buttonsLayout.setLayoutParams(params);
buttonsLayout.setPadding(10, 5, 10, 5);
buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonsLayout.setWeightSum(2);
rg.addView(buttonsLayout);
}
buttonsLayout.addView(radioButton);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static RadioButton CreateRadioButton(Context context, FormData.QuestionChoice questionChoice, int size) {
RadioButton rb = new RadioButton(context);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
rb.setLayoutParams(params);
rb.setTag(questionChoice.getChoiceId());
rb.setButtonTintList(getTintColor(context));
rb.setId(Integer.parseInt(questionChoice.getChoiceId()));
rb.setText(questionChoice.getChoice());
rb.setTextColor(getTintColor(context));
return rb;
}
public static MultiRowsRadioGroup CreateRadioGroup(Context context, FormData.QuestionDetail questionDetail) {
MultiRowsRadioGroup rg = new MultiRowsRadioGroup(context);
rg.setOrientation(LinearLayout.VERTICAL);
rg.setPadding(10, 10, 10, 10);
rg.setId(Integer.parseInt(questionDetail.getQuestionId()));
rg.setTag(questionDetail.getQuestionId());
return rg;
}
Can you check what is wrong with this code? Thanku