2

I have a LinearLayout (vertical) with two child views in it. The 1st one is a ScrollView and the 2nd one is another layout with Visibility.GONE, its size is not fixed (determined by its children).

<LinearLayout vertical>

    <ScrollView> ... </ScrollView>

    <AnotherLayout visibility=GONE height=wrap_content> ... </AnotherLayout>

</LinearLayout>

At some point of time I want to show AnotherLayout. But, once it pops up, I also want to adjust the scrolling of my ScollView one. For this, I need to know the size of this AnotherLayout.

I'm doing something like that:

int oldHeight = scrollArea.getHeight();
linearLayout.setVisibility(VISIBLE);
int newHeight = scrollArea.getHeight();

But oldHeight and newHeight are still the same.

How can I calculate the new height?

stillwaiting
  • 415
  • 1
  • 5
  • 14

1 Answers1

0

The two dimensions are the same because visibility change took time and the line of code was run before that so it returns the same. You can use a visibility listener to calculate the dimension after visibility change , You may use that

    linearLayout.setTag(linearLayout.getVisibility());
    linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int newVis = linearLayout.getVisibility();
            if((int)linearLayout.getTag() != newVis)
            {
                linearLayout.setTag(linearLayout.getVisibility());
                //visibility has changed
                int newHeight = scrollArea.getHeight();
            }
        }
    });
Daniel Raouf
  • 2,307
  • 1
  • 22
  • 28