Android examples for android.widget:ListView
set ListView Parent Height Based On Children
import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; public class Main{ public static void setListViewParentHeightBasedOnChildren( ListView listView, int attHeight) { ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = (int) (getListViewHeightBasedOnChildren(listView) + attHeight + 0.5);//from w w w.j a va 2s. com listView.setLayoutParams(params); LinearLayout linearLayout = (LinearLayout) listView.getParent(); linearLayout.setLayoutParams(params); } public static int getListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return 0; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); int itemHeight = listItem.getMeasuredHeight(); totalHeight += itemHeight; } int height = (int) (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + 0.5); return height; } }