title: Set ListView Height dynamically based on multiline textview inside it
body: <p>I am using a base adapter to set data in a listview dynamically. I tried to set listview height dynamically. It works perfectly if the textview inside it is single line. However if the textview is multiline, the height is not set properly. The height is set considering single line textview only. How can I set correctly set the height of listview items containing multiline textview. Here's the code:</p>
  
  <p>Fragment code:</p>
  
  <pre><code>ListView mlistNews=(ListView)rootView.findViewById(R.id.news_listView);
  mlistNews.setAdapter(new NewsAdapter(getActivity(),params_news));
  Utils.setListViewHeightBasedOnItems(mlistNews);
  </code></pre>
  
  <p>Utility function:</p>
  
  <pre><code>public static boolean setListViewHeightBasedOnItems(ListView listView) {
  
      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(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
              totalItemsHeight += item.getMeasuredHeight();
          }
  
          // Get total height of all item dividers.
          int totalDividersHeight = listView.getDividerHeight() *
                  (numberOfItems - 1);
          // Get padding
          int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
  
          // Set list height.
          ViewGroup.LayoutParams params = listView.getLayoutParams();
          params.height = totalItemsHeight + totalDividersHeight + totalPadding;
          listView.setLayoutParams(params);
          listView.requestLayout();
  
          return true;
  
      } else {
          return false;
      }
  
  }
  </code></pre>
  
  <p>dashboard_list_news.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="5dp"&gt;
  &lt;TextView
      android:layout_width="0dp"
      android:id="@+id/news_item_text"
      android:layout_weight="1"
      android:layout_height="wrap_content"
      android:layout_gravity="left|center_vertical"
      android:layout_marginLeft="5dp"/&gt;
  
  &lt;ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/chevron"
      android:layout_gravity="center_vertical|right"
      android:layout_marginLeft="5dp"
      android:layout_marginRight="10dp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>listView:</p>
  
  <pre><code>&lt;ListView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/rounded_shape_list"
              android:id="@+id/news_listView"
              android:paddingTop="5dp"
              android:paddingBottom="5dp"
              android:divider="#6DCB99"
              android:dividerHeight="1dp" /&gt;
  </code></pre>