LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(btn);
TextView view = new TextView(this);
view.setText("1. Select the correct statement in the following options :");
layout.addView(view);
int n = 5;
for (int i = 0; i < n; i++) {
CheckBox checkedTextView=new CheckBox(this);
checkedTextView.setText(String.valueOf(i));
checkedTextView.setId(i);
checkedTextView.setOnCheckedChangeListener(this);
layout.addView(checkedTextView);
}
LinearLayout layout2 = (LinearLayout) findViewById(R.id.ll);
layout2.addView(layout);
TextView view1 = new TextView(this);
view1.setText("2. Select one correct statement in the following options :");
layout.addView(view1);
RadioButton[] radioButtons=new RadioButton[5];
RadioGroup group=new RadioGroup(this);
group.setOrientation(RadioGroup.HORIZONTAL);
for (int i = 0; i < n; i++) {
radioButtons[i]=new RadioButton(this);
group.addView(radioButtons[i]);
radioButtons[i].setText(String.valueOf(i));
radioButtons[i].setId(i);
}
layout.addView(group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
System.out.println("the checked radio button id is :"+checkedId);
}
});
}
@Override
public void onCheckedChanged(CompoundButton cb,boolean isChecked) {
Toast.makeText(getApplicationContext(),String.valueOf(cb.getId()) , Toast.LENGTH_LONG).show();
}