title: How to calculate the height of a scrollView using ViewTreeObserver?
body: <p>I have a ScrollView with some layouts in it, called the <code>fragment_about_sl.xml</code>.And the class associated to it is called <code>AboutSLFragment.java</code>.I want to calculate the height of the scrollView to get height ratio for an image.I researched the web and found this code.</p>
  
  <pre><code>ViewTreeObserver vto = scrollView.getViewTreeObserver(); 
  vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
  @Override 
  public void onGlobalLayout() { 
  scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  
  int scrollViewHeightInPixels = layout.getMeasuredHeight(); 
  
  //This is equal with %45 weight you tried before
  int height = (scrollViewHeightInPixels * 45) / 100;
  ViewGroup.LayoutParams pagerParams = viewPager.getLayoutParams();
  pagerParams.height = height;
  } 
  });
  </code></pre>
  
  <p>As shown in the above code.It just have said <code>scrollView</code> instead of getting it from <code>findViewById()</code>.And also in the below code,it just says <code>layout.getMeasuredHeight()</code> instead of specifying the name of layout.</p>
  
  <pre><code>int scrollViewHeightInPixels = layout.getMeasuredHeight(); 
  </code></pre>
  
  <p><strong>The question is that I want to know what is meant by layout here(above).What should I put there,is it the id of the scrollView?If so I developed a code myself and I want to know whether it is correct.Please help me I am new to android.The id of the scrollview I used is aboutslscrollview.</strong></p>
  
  <pre><code>final View view = rootView.findViewById(R.id.aboutslscrollview);
  ViewTreeObserver vto = view.getViewTreeObserver();
  vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
          view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  
          int scrollViewHeightInPixels = view.getMeasuredHeight();
  
          //This is equal with %45 weight you tried before
          int height = (scrollViewHeightInPixels * 45) / 100;
          ViewGroup.LayoutParams pagerParams = viewPager.getLayoutParams();
          pagerParams.height = height;
      }
  });
  </code></pre>