The format of my XML file is as following:
LinearLayout
ScrollView
RelativeLayout
LinearLayout
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);
}
}