title: How to Set ListView Height Depending on the Items inside ScrollView
body: <p>I'm trying to add <code>listView</code> inside <code>scrollView</code> and.</p>
  
  <p>the problem is I can't set the <code>ListView</code> height programmatically and correctly redraw the <code>ScrollView</code>, as the items below it still in the old position before changing the height.</p>
  
  <p>XML</p>
  
  <pre><code>&lt;ScrollView
      android:id="@+id/movie_detail_scroll_view"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"&gt;
  
      &lt;RelativeLayout
          android:id="@+id/movie_detail"
          style="?android:attr/textAppearanceLarge"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:clipChildren="false"&gt;
          ...........
          &lt;ListView
              android:id="@+id/trails_list_view"
              android:layout_width="match_parent"
              android:layout_height="200dp"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_below="@+id/movie_videos_container"
              android:choiceMode="none"/&gt;
          &lt;include
              android:id="@+id/movie_reviews_container"
              layout="@layout/partial_movie_reviews"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/trails_list_view"/&gt;
          ............
      &lt;/RelativeLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>the method that I used to resize the listView:</p>
  
  <pre><code>/**
   * Sets ListView height dynamically based on the height of the items.
   *
   * @param listView to be resized
   * @return true if the listView is successfully resized, false otherwise
   */
  public static boolean setListViewHeightBasedOnItems(ListView listView, ViewGroup container) {
  
      ListAdapter listAdapter = listView.getAdapter();
      if (listAdapter != null) {
  
          int numberOfItems = listAdapter.getCount();
  
          // Get total height of all items.
          int totalItemsHeight = 0;
          for (int itemPos = 0; itemPos &lt; numberOfItems; itemPos++) {
              View item = listAdapter.getView(itemPos, null, listView);
              item.measure(0, 0);
              totalItemsHeight += item.getMeasuredHeight();
          }
  
          // Get total height of all item dividers.
          int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);
  
          // Set list height.
          ViewGroup.LayoutParams params = listView.getLayoutParams();
          params.height = totalItemsHeight + totalDividersHeight;
          listView.setLayoutParams(params);
          listView.requestLayout();
  
          //redraw the container layout.
          container.requestLayout();
  
          return true;
  
      } else {
          return false;
      }
  
  }
  </code></pre>
  
  <p>the result:
  <a href="https://i.stack.imgur.com/wk6qm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/wk6qm.png" alt="enter image description here"></a></p>
  
  <p>I can't get rid of this free space even if when I redraw the <code>ScrollView</code></p>
  
  <p>what should I do?</p>