0

I am trying to add several custom ViewGroups dynamically to an existing RelativeLayout through a ForLoop.

When I add them, nothing is displayed. Maybe it's because the ViewGroup isn't finished loading before it's added?

Anyway to fix this?

Thank you,

GameActivity.java:

hScrollItems = (RelativeLayout) findViewById(R.id.hScrollItems);

    for (int i = 0; i < 5; i++){
        int w = getResources().getInteger(R.integer.GridItemWidth);
        int h = getResources().getInteger(R.integer.GridItemWidth);
        GridItem gi = new GridItem(getBaseContext(), w, h, GridType.EMPTY);

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.setMargins(w * i, 0, 0, 0);

        hScrollItems.addView(gi, params);
    }

GridItem.java

public GridItem(Context context, int Width, int Height, GridType Type) {
    super(context);

    /*LayoutParams params = new LayoutParams(Width, Height);
    this.setLayoutParams(params);*/

    this.setBackgroundColor(0xFFFFFF33);

    ImageView img = new ImageView(context);
    img.setBackgroundResource(R.drawable.ic_launcher);
    this.addView(img);
}
public enum GridType {
    EMPTY("EMPTY"),
    HOUSE("HOUSE");
    private String value;
    private GridType(String value) {
       this.value = value;
    }
    public String getValue() {
       return value;
    }
}
Tom
  • 43
  • 7

2 Answers2

1

getBaseContext() isn't recommended for this. Since it's being called from an Activity class, you should just use this, since Activity extends Context.

GridItem gi = new GridItem(this, w, h, GridType.EMPTY);

Also, I don't see where you are setting any ids or alignment information for the children. Views in a RelativeLayout need to have their ids set, and refer to something so it knows where to put them.

After that, try a call to requestLayout() on the parent.

Community
  • 1
  • 1
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • I've tried changing it, but no result, still nothing displayed. – Tom Oct 15 '12 at 14:12
  • I've tried setting IDs for every GridItem and adding hScrollItems.requestLayout() but still nothing displayed... :( – Tom Oct 15 '12 at 14:36
  • When I try to add regular ImageViews in the same way as above, it works and I see a graphical display. But adding my own GridItem class, where I put an ImageView in myself, gets no display. Anyone please help.. – Tom Oct 15 '12 at 15:06
  • Ok I changed the `GridItem` class to extend to `RelativeLayout` instead of `ViewGroup`, and now I get graphical feedback on `this.setBackgroundColor(0xFFFFFF33);` but I don't see the `ImageView`. – Tom Oct 15 '12 at 15:28
  • For something that simple, a `LinearLayout` may work better. If you just need something to wrap views in, `RelativeLayout` comes with too much baggage(having to set IDs and manage child/parent alignment). You may also want to explicitly set the `ImageView`'s params. – Geobits Oct 15 '12 at 15:31
  • Thank you, I'll use that. I also submitted my answer as the fix for seeing the ImageView. – Tom Oct 15 '12 at 15:56
  • K, but if you change it to LinearLayout, you may want to try dropping the call to `bringToFront()`. I create views all the time, and I've never needed to use that for them to appear. You really shouldn't need it, since the layout should only have one child, so "front" doesn't really mean anything. – Geobits Oct 15 '12 at 15:59
  • I understand, that's why I was surprised that that 'fix' worked.. Without it, I couldn't see the child. – Tom Oct 15 '12 at 16:02
0

Ok I fixed it!

First of all, I had to change GridItem to a RelativeLayout instead of a ViewGroup.

Second, I had to bring the ImageView in GridItem to front via img.bringToFront().

Tom
  • 43
  • 7