0

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.

DevOfZot
  • 1,362
  • 1
  • 13
  • 26

2 Answers2

-1

You must add your ImageViews after setting properties them.

Move this code to end of the for loop:

layoutFoo.addView(foo[i]);

By the way you don't need to ImageView[]. You can accomplish it with one ImageView.

UPDATE:

Here is the code you can do same thing with:

ImageView foo;
RelativeLayout.LayoutParams fooParams;
for (int i = 0; i < levels; i++) { 
    foo = new ImageView(context);
    foo.setId(10 + i);
    fooParams = (RelativeLayout.LayoutParams) foo.getLayoutParams(); // I don't understand why you are trying to get params of which hasn't been specified before.
    // If I were you, I would do like:
    fooParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    if (i == 0) {
        fooParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    } else {
        fooParams.addRule(RelativeLayout.RIGHT_OF, foo.getId()-1);
    }
    foo.setImageBitmap(yourBitmap); // Set bitmap.
    foo.setLayoutParams(fooParams);
    layoutFoo.addView(foo);
}
JustWork
  • 1,944
  • 3
  • 22
  • 34
  • Unless I'm missing something, this is not true; attempting to add a Rule to a view that hasn't been added to a layout throws a RuntimeException. – DevOfZot Sep 04 '13 at 01:02
  • Also, could you expand on that second bit? Are you suggesting that I can add several bitmaps to an ImageView? I don't see any methods for that... – DevOfZot Sep 04 '13 at 01:08
  • Unfortunately you are missing so much. It's just one example which I found in 5 seconds and proves that you are missing: http://stackoverflow.com/a/2397869/2371858 – JustWork Sep 04 '13 at 01:08
  • Your answer, "You must add your ImageViews after setting properties them", turned out not to be the case; adding an ImageView to a layout before setting the properties works fine. However, thanks for the working sample code which did lead me to the answer, which was that I was not setting a height or width for the ImageView. – DevOfZot Sep 04 '13 at 01:59
  • It's good if it did lead you. By the way, why -1 even it's marked as answer? – JustWork Sep 04 '13 at 02:05
-1

I think the easiest way to place those images will be using a gallery. Although it's deprecated, I think it's a better idea to do it that way.

mike20132013
  • 5,357
  • 3
  • 31
  • 41