2

I've created checkboxes programmatically, and i need to set layout_above for each of them( so they will be above each other). this code works in loop, so it will generate few checkboxes, but i need to set id. how can I properly generate it?

CheckBox checkBox = new CheckBox(getApplicationContext());
     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
        (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ABOVE, checkBox.getId());
        checkBox.setLayoutParams(layoutParams);
        checkBox.setWidth(getScreenWidth());
        checkBox.setText(questions.get(questionId).answers.get(index));
    ll.addView(checkBox);
Irshu
  • 8,248
  • 8
  • 53
  • 65

2 Answers2

1
   int CHECK_BOX_ID = 1;
 CheckBox checkBox = new CheckBox(getApplicationContext());
 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
    (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.ABOVE, checkBox.getId());
    checkBox.setLayoutParams(layoutParams);
    checkBox.setWidth(getScreenWidth());
    checkBox.setId(CHECK_BOX_ID);
    CHECK_BOX_ID++;
    checkBox.setText(questions.get(questionId).answers.get(index));
ll.addView(checkBox);
Atahar Hossain
  • 336
  • 3
  • 11
0
 checkBox.setId(checkBoxId);

generate a random number and increment it within the loop. This should probably do it.

Tushar Saha
  • 1,978
  • 24
  • 29