@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int totalHeight = 0; ListAdapter adapter = getAdapter(); for (int i = 0; i < adapter.getCount(); i++) { View listItem = adapter.getView(i, null, this); listItem.measure(widthMeasureSpec, heightMeasureSpec); totalHeight += listItem.getMeasuredHeight(); } totalHeight += getDividerHeight() * (adapter.getCount() - 1); setMeasuredDimension(widthMeasureSpec, totalHeight); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightSize = MeasureSpec.getSize(heightMeasureSpec); int maxHeight = 200; // set maximum height to 200 pixels int newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, newHeightMeasureSpec); int height = getMeasuredHeight(); setMeasuredDimension(widthMeasureSpec, heightSize < height ? heightSize : height); }The android.widget.ListView is part of the Android SDK and does not require any additional library package.