0

I am programming an app which generates all of its layout programmatically. I am looking to place an array of ImageView[] depending on their index. But it looks like the LayoutParams doesn't do anything since all the ImageViews are being superposed. Here is the code I am using:

for (int i=0; i<tiles; i++){
        Log.i("Setting up tile", "#" + i);
        tileParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
        tileParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
        tileParams.removeRule(RelativeLayout.BELOW);
        tileParams.removeRule(RelativeLayout.RIGHT_OF);
        if (((double)i/columns) == Math.round(i/columns)){
            if (i == 0){
                tileParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                tileParams.addRule(RelativeLayout.ALIGN_PARENT_START);
            } else {
                tileParams.addRule(RelativeLayout.BELOW, tileView[i-columns].getId());
                tileParams.addRule(RelativeLayout.ALIGN_PARENT_START);
            }
        } else {
            tileParams.addRule(RelativeLayout.RIGHT_OF, tileView[i-1].getId());
        }
        tileView[i].setLayoutParams(tileParams);
        grid.addView(tileView[i]);
}

Here, "grid" is a RelativeLayout and "tileParams" is a RelativeLayout.LayoutParams.

PS: I know that the if methods are used correctly at the required time, and the ImageViews have their ids set using iv.setId(View.generateViewId()).

  • what is the reason of doing this that way? i mean by calling `addRule` / `removeRule` from your java code? how do you want to layout your views? – pskink Aug 07 '17 at 11:10
  • I call `removeRule` because the `LayoutPrams` is initialized outside the `if` method –  Aug 07 '17 at 11:12
  • You should probably create a new LayoutParams every iteration. Currently all views share the same object so they all get the same rules, that of the last tile. – RobCo Aug 07 '17 at 11:12
  • That's why I removeRules –  Aug 07 '17 at 11:17
  • but `setLayoutParams` does not deep copy the input parameter, so on every iteration the same layout param is used – pskink Aug 07 '17 at 11:18
  • I just tried to make 3 different `layoutParams` as you suggested, but the `ImageView`s are still superposed. –  Aug 07 '17 at 11:26
  • how do you want to layout your views? cannot you use other existing layouts? – pskink Aug 07 '17 at 11:27
  • @pskink There are two parameters passed with an Intent: int rows and int columns. Then `ImageView[rows*columns]` is initialized and the images are displayed in a table-like layout. I already tried to use a `TableLayout` but same thing happens. And this if method just works fine to indentify where images should be placed. –  Aug 07 '17 at 11:31
  • so if this is so simple grid then make a custom `ViewGroup` class and override `onLayout` (and maybe `onMeasure`) method - inside `onLayout` iterate over each child view and call `layout` method – pskink Aug 07 '17 at 11:34
  • Doesn't `onLayout` work the same way as `LayoutParams`? –  Aug 07 '17 at 11:39
  • if you dont want a custom `ViewGroup` then use something like [this](https://pastebin.com/raw/D7fVMukh) – pskink Aug 07 '17 at 12:50

0 Answers0