Wowzer
Wowzer

Reputation: 1161

Why my custom viewgroup won't show the textview within the linear layout?

I am not so sure why my custom viewgroup won't render the textview wihtin its child which is a linear layout:

This is my viewgroup code:

import android.content.Context;
import android.graphics.Color;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewGroup;

public class noteLayout extends ViewGroup {
    float leftOrientationSize = 0;
    float rightOrientationSize=0;
    public noteLayout(Context activityContext)
    {
        super(activityContext);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int numberOfChild = getChildCount();
        for (int i = 0;i<numberOfChild;i++){
            View childView = getChildAt(i);
            float childHeight = (float) childView.getMeasuredHeight();
            float childWidth = (float) childView.getMeasuredWidth();
            RectF rect = new RectF();
            rect.bottom = 300;
            rect.top = 20;
            rect.left = 50;
            rect.right = (getWidth()/2)-20;
            childView.layout((int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom);
        }
    }
}

I have created a simple linear layout with a textview inside. This file is in xml and it is called test3.

Also, the following code is used to add the test3 layout into the custom viewgroup.

   private void layoutNoteView() {
        noteLayout noteLayout = new noteLayout(this);
        noteLayout.addView((View) getLayoutInflater().inflate(R.layout.test3,null));
        RelativeLayout.LayoutParams noteLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        noteLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            noteLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
        }
        noteLayoutParams.addRule(RelativeLayout.BELOW, R.id.StartRelativeLayout);
        relativeLayoutMain.addView(noteLayout,noteLayoutParams);
    }

My question is I am not so sure why my custom viewgroup won't even render the textview inside the linear layout. All it renders is just the linear layout of the test3 layout file. I am so desperately trying to figure out how can I get the textview to get rendered.

Please note that if I have a textView alone without putting it into the linearlayout, my custom viewgroup renders that textview perfectly fine. It only happens when I have it within a layout and then put it in my custom viewgroup layout.

Upvotes: 0

Views: 895

Answers (1)

David Medenjak
David Medenjak

Reputation: 34542

Drawing a ViewGroup to the screen requires 2 steps:

  1. measure the ViewGroup and its children to get the correct size

  2. layout the children where they should draw themselves

Measure is about finding out how big a view is. After measuring, you should have a value indicating how big a view needs to be.

Layout is about actually assigning position and size to a view. You can, but shouldn't, ignore the values returned by the measurement. Making it bigger usually is no problem, but making it smaller will most likely cut something off.

By not calling the measuring methods on the children of your ViewGroup they have no knowledge of how big they can be, or how much space they will get.

Just layouting a TextView to the full width/height will render correctly, since it gets enough size to render correctly.

Layouting a LinearLayout without prior measurement, the LinearLayout itself has no knowledge about its children. For layouting purpose, it is looking at getMeasuredHeight() but will find no value there, hence be not able to layout its children. This is why the LinearLayout will 'draw' but its children not.


Always include measurement, or extend from an existing layout that you want to extend, e.g. FrameLayout if you just want to set positions yourself, but children may overlap. Without measuring, some things will work fine, while others won't, and this would mostly just be trial and error about finding out what will.

Upvotes: 1

Related Questions