Expand ViewGroup after adding children

20 views Asked by At

I'm trying to add children to LinearLayout programmaticaly but LinearLayout does not expand more then screen size. However if I hardcode the height - then it is created more then screen size. But I want to use WRAP_CONTENT height and the height to be calculated depends on the sum of the children heights. Is that possible? Here is an example:

           LinearLayout outerContainer = new LinearLayout(this);

        outerContainer.setOrientation(LinearLayout.VERTICAL);
        outerContainer.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        LinearLayout mViewHolder = new LinearLayout(this);
        mViewHolder.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mViewHolder.setOrientation(LinearLayout.VERTICAL);
        mViewHolder.setId(R.string.rootHeadlineContainer);

        for (int i=0; i< 28; i++){
            TextView header = new TextView(this);
            header.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            header.setBackgroundColor(Color.GRAY);
            header.setTextSize(36f);
            header.setText("  Header");
            outerContainer.addView(header);
        }

        outerContainer.addView(mViewHolder);
        outerContainer.requestLayout();
        //end test

        LinearLayout rootHolder = new LinearLayout(this);

        rootHolder.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); //todo 
        rootHolder.addView(outerContainer);
        rootHolder.setY(pos);
        scrollView.addView(rootHolder);
        rootHolder.requestLayout();

I can set for example:

rootHolder.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 10000));

Then it expands to 10000 height and has enought space for all the children. But if I use WRAP_CONTENT then I got rootHolder with the screen height and it clip the children enter image description here

0

There are 0 answers