The format of my XML file is as following:
LinearLayout
ScrollView
RelativeLayout
LinearLayout
import android.view.ViewGroup.LayoutParams;
ListView mListView = (ListView) findViewById(R.id.listviewid);
LayoutParams list = (LayoutParams) mListView.getLayoutParams();
list.height = set the height acc to you;//like int 200
mListView.setLayoutParams(list);
this class can be use to expand the height of listview or gridview at runtime ,please put it in your package and use by passing listview
public class CustomView {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
public static void getGridViewSize(GridView myGridView) {
ListAdapter myListAdapter = myGridView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
int maxHeight = 0;//implement new
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myGridView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight()+8;
if(maxHeight < listItem.getMeasuredHeight()){//implement new
maxHeight = listItem.getMeasuredHeight()+8;
System.out.println(" max height of gridview item<11> "+size+" == "+maxHeight);
}
System.out.println(myListAdapter.getCount()+" height of gridview item<22> "+size+" == "+listItem.getMeasuredHeight());
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myGridView.getLayoutParams();
if(myListAdapter.getCount() > 1)
params.height = maxHeight * ((myListAdapter.getCount()+1)/2);//implement new
else
params.height = maxHeight;//implement new
myGridView.setLayoutParams(params);
}
}
Try this answer,
public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
@Override
public void run() {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
Log.d("listItemHeight" + listItem.getMeasuredHeight(), "___________");
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (int) ((totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))));
listView.setLayoutParams(params);
listView.requestLayout();
}
});
}
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<ListView
android:id="@+id/lv_premimum_upgrade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/custom_margin"
android:minHeight="48dp">
</ListView>
</LinearLayout>
</ScrollView>
int list_cell_size=188; // cell_size is varible
ViewGroup.LayoutParams list;
list = (ViewGroup.LayoutParams) listView.getLayoutParams();
list.height = list_cell_size*response.size(); // response is list items size
listView.setLayoutParams(list);
Its not recommended to use ListView inside of a ScrollView
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}