6

How to set Height of ListView.

XML Code (This code is working fine and fixed height as 200 dip)

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/dialog_ListView"
        android:layout_width="wrap_content"
        android:layout_height="200dip"
        android:padding="5dip"
        android:layout_weight="1"
        android:dividerHeight="5dip"
        android:divider="@android:color/transparent" />        
</LinearLayout>

Android Code (This code not fixed width and height)

convertView = mInflater.inflate( R.layout.dialog_page, null );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );

Android Code (This code i got Error Message)

convertView = mInflater.inflate( R.layout.lock_file_page, parent );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );

Error Message

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

How do fix Width and Height at Runtime.

Thanks in advance.

Sekar
  • 1,061
  • 7
  • 21
  • 38

4 Answers4

29

Try This code..

 LayoutParams lp = (LayoutParams) mListView.getLayoutParams();
 lp.height = 300;
 mListView.setLayoutParams(lp);
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
Mehul Santoki
  • 1,208
  • 1
  • 12
  • 25
3
public static boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
                (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        return true;

    } else {
        return false;
    }

}
Andres Marin
  • 251
  • 3
  • 12
1

First Remove import files for LayoutParams and after that Use below code for LayoutParams.

LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(200, 300);
convertView.setLayoutParams(params);
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
  • This code not working. I got same error message `java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams` – Sekar Jul 13 '12 at 13:18
  • @user1474868 please post your full code with import statements, so i will give you proper solution. – Dipak Keshariya Jul 14 '12 at 04:39
0

try this

public void setDynamicHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
    return;
    }
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);

    if(listItem != null){
        listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        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();}
Zahra.HY
  • 1,684
  • 1
  • 15
  • 25