I'm trying to add five or so ImageViews to a RelativeLayout, with each new one being to the right of the previous one. However, what I get is (apparently) all of them stacked up on each other, as if they were all ALIGN_PARENT_LEFT. Relevant code snippet:
ImageView[] foo = new ImageView[levels];
for (int i = 0; i < levels; i++) {
foo[i] = new ImageView(context);
layoutFoo.addView(foo[i]);
foo[i].setId(10 + i);
if (i == 0) {
RelativeLayout.LayoutParams fooParams = (RelativeLayout.LayoutParams) foo[i].getLayoutParams();
fooParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
foo[i].setLayoutParams(fooParams);
} else {
RelativeLayout.LayoutParams fooParams = (RelativeLayout.LayoutParams) foo[i].getLayoutParams();
fooParams.addRule(RelativeLayout.RIGHT_OF, foo[i-1].getId());
foo[i].setLayoutParams(fooParams);
}
}
Any hints what I'm doing wrong? It shouldn't matter that the ImageViews don't have any width (because I haven't assigned a bitmap to them) yet, right?
Edit: the problem turned out to be not setting a height and width for the ImageView. Solution is to set them either with setLayoutParams() or with the constructor as in JustWork's example.