Set ListView Height Programmatically
This will help you.
ListAdapter listadp = listview.getAdapter();
if (listadp != null) {
int totalHeight = 0;
for (int i = 0; i < listadp.getCount(); i++) {
View listItem = listadp.getView(i, null, listview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listview.getLayoutParams();
params.height = totalHeight + (listview.getDividerHeight() * (listadp.getCount() - 1));
listview.setLayoutParams(params);
listview.requestLayout();
you can try this function to update the height of listView
public static void updateListViewHeight(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
return;
}
// get listview height
int totalHeight = 0;
int adapterCount = myListAdapter.getCount();
for (int size = 0; size < adapterCount; size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// Change Height of ListView
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = (totalHeight
+ (myListView.getDividerHeight() * (adapterCount));
myListView.setLayoutParams(params);
}