Java tutorial
//package com.java2s; import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import android.widget.ListView; public class Main { public static void setListViewSize(ListView list) { ListAdapter myListAdapter = list.getAdapter(); if (myListAdapter == null) { //do nothing return null return; } //set listAdapter in loop for getting final size int desiredWidth = View.MeasureSpec.makeMeasureSpec(list.getWidth(), View.MeasureSpec.UNSPECIFIED); int totalHeight = 0; View view = null; for (int i = 0; i < myListAdapter.getCount(); i++) { view = myListAdapter.getView(i, view, list); if (i == 0) { view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); } view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight(); } //setting listview item in adapter ViewGroup.LayoutParams params = list.getLayoutParams(); params.height = totalHeight + (list.getDividerHeight() * (myListAdapter.getCount() - 1)); list.setLayoutParams(params); } }