0

So, for practice, i'm learning how to programatically create views. I've created a new layout extending Viewgroup (which I called Custom1) which puts children views (all of the same size) in two columns.

The children of this group are also a custom layout (which I called Custom2) containing an imageview, and two textviews. I use a for loop to add the necessary amount of views to the viewgroup, and the onLayout is overriden.

Now, I tried to run this on a Nexus 4 with the "show layout bounds" option checked. I can see the bounds of the children of the Custom1 are all in the right place, and based on the log, the children of custom2 are also in the correct place. However, only the first "custom2" is showing properly (I.e. the first custom2 shows an imageView and two textviews, the rest are empty).

Is it possible that the parent view is covering up the children views?

If not, has anybody run into a similar issue before?

Here is some of my code for custom1:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    if (ItemToDebug.equals("Layout")){
        if (count == numberOfChannels){
            Log.d("Layout:", "Number of children matches number of channels");
        }else{
            Log.d("Layout:", "Mismatch between number of children and number of channels");
        }
        Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
    }

    for (int i = 0; i < count; i++){
        View child = getChildAt(i);
        if (i%2 == 0){
            if (LayoutLeftChild(i/2, l, t, r, b, child)){ //Lays out Child, returning a Boolean if successful
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "successful");
                }
            } else
            if (ItemToDebug.equals("Layout")){
                Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "failed");
            }
        }
        if (i%2 == 1){
            if (LayoutRightChild(i/2, l, t, r, b, child)){
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "successful");
                }
            }else{
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "failed");
                }
            }
        }
    }

}


/*
Left edge for right column = l + (r-l)/2 + 15
Right edge for right column = r - 20
20dp Margin between rows
Rows are 400 dp tall
 */
private boolean LayoutRightChild(int i, int l, int t, int r, int b, View child) {
    final View Child = child;

    final int Left = l + (r-l)/2 + 15;
    final int Right = r - 20;
    final int Top = t + i*20 + (i-1)*400;
    final int Bottom = Top + 400;

    Child.layout(Left, Top, Right, Bottom);
    if (ItemToDebug.equals("Layout")){
        Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
    }

    return true;
}



/*
Left edge for left column = l + 20
Right edge for left column = l + (r-l)/2 - 15
20dp Margin between rows
Rows are 400 dp tall
 */
private boolean LayoutLeftChild(int i, int l, int t, int r, int b, View child) {
    final View Child = child;

    final int Left = l + 20;
    final int Right = l + (r-l)/2 - 15;
    final int Top = t + i*20 + (i-1)*400;
    final int Bottom = Top + 400;

    Child.layout(Left, Top, Right, Bottom);

    if (ItemToDebug.equals("Layout")){
        Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
    }
    return true;
}

Here is some code from custom2:

 @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    final int count = getChildCount(); //There should be three children - one ImageView on top and two TextViews on bottom
    if (ItemToDebug.equals("Layout")){
        if (count == 3){
            Log.d("View Contents:", "3 Children Views found");
        }else{
            Log.d("View Contents:", "Number of Children Incorrect. " + Integer.toString(count) + " children found.");
        }
        Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
    }

    //Get children here in for loop and place.
    for (int i = 0; i < count; i++){
        final View child = this.getChildAt(i);

        //Layout should already have margins, so align contents with left and right sides.
        int width = r - l;
        int top;
        int height;
        switch(i){
            case 0:
                top = t;
                height = 100;
                if (ItemToDebug.equals("Layout")) {
                    Log.d("Layout:", "Image Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
                }
                break;
            case 1:
                top = t + 100;
                height = 60;
                if (ItemToDebug.equals("Layout")) {
                    Log.d("Layout:", "TextView (nowPlaying) Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
                }
                break;
            case 2:
                top = t + 160;
                height = 60;
                if (ItemToDebug.equals("Layout")) {
                    Log.d("Layout:", "TextView (nextPlaying) Laid Out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
                }
                break;
            default:
                top = t;
                height = 0;
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "More than 3 children have been added to the Custom2");
                }
                break;
        }

        child.layout(l, top, r, top + height);

    }
}

Here is my log:

https://drive.google.com/file/d/0B0EGM_1_9jazWEZ1RXh4a2VXdnM/view?usp=sharing

And the screenshot.

Screenshot

Kongo
  • 71
  • 1
  • 2
  • 12

1 Answers1

0

The answer can be found here: Views within a Custom ViewGroup are not showing

Children are laid our wrt their parents (left and top are 0)

Community
  • 1
  • 1
Kongo
  • 71
  • 1
  • 2
  • 12