0

I would like to set the Height of my ScrollView per Code dynamically cause my ScrollView is actually higher then it shall be (empty space at bottom).

My thoughts were, that I could get the Heights of all Controls within the the ScrollView, sum-up them and then I could set that Height to my ScrollView.

What I tried is following code:

protected override void OnStart()
{
    base.OnStart();
    SetScrollViewSize();
}

private void SetScrollViewSize()
{
    var root = FindViewById<ScrollView>(Resource.Id.root);
    if (root != null)
    {
        var controls = GetSelfAndChildrenRecursive(root); //Gives me all Controls in the root (The ScrollView)
        int heightOfAllControlsTogether = 0;
        foreach (ViewGroup control in controls)
        {
            heightOfAllControlsTogether += control.Height;
        }
        ViewGroup.LayoutParams parameters = new ViewGroup.LayoutParams(root.Width, heightOfAllControlsTogether);
        root.LayoutParameters = parameters;
    }
}

The Heights and MeasuredHeights are always 0 (zero) - (I know it needs to be rendered first, but what would be the right place then?) and I'm not even sure if my approach would work.

Any Help would be appreciated!

eMi
  • 5,540
  • 10
  • 60
  • 109
  • How exactly do you get that space at the bottom(out of curiosity are you using layout gravity for some of your children)? In the pure java sdk you would post a `Runnable` on one of the views in the `onCreate` method or use something like `scrollView.getViewTreeObserver().addGlobalLayoutListener() {/*calculate here*/}`. But I don't know how mono works. – user Mar 14 '13 at 14:11
  • That space comes since I use several ListViews in my ScrollView. As we all know, its not recommended to use ListViews in a ScrollView but I really needed that. I'm using this code here: http://stackoverflow.com/a/3495908/880472 it works fine but it gives me strange space at the bottom, what I'm trying to solve now. – eMi Mar 14 '13 at 14:15
  • The accepted answer on that question comes from one of the Android SDK engineers and he says you shouldn't do that. Anyway, check mono for that global layout(I don't work with mono). If it exists attach if to a view and you should have the right dimensions of the views. – user Mar 14 '13 at 14:18

2 Answers2

0

Well I found out why.

The Background-Graphic was too high. That was creating the empty space at the bottom.

eMi
  • 5,540
  • 10
  • 60
  • 109
0

THis is an example to set scrollview height based on total height of Children:

private void setScrollViewHeightBasedOnChildren() {

int size = layoutRoot.getChildCount();
LinearLayout item = (LinearLayout) layoutRoot.getChildAt(0);
item.measure(0, 0);
int height = item.getMeasuredHeight();
//Reset size of ScrollView
scroll.setMinimumHeight(height / size);
scroll.invalidate();
}

See details: Set the height of ScrollView equal total height of children

hungtdo
  • 661
  • 7
  • 15