update ListView Height - Android User Interface

Android examples for User Interface:ListView

Description

update ListView Height

Demo Code


//package com.java2s;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ListAdapter;
import android.widget.ListView;

public class Main {
    public static void updateListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }//from   www  .ja va 2  s.  c o  m

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    public static View getView(int id, LayoutInflater inflater,
            ViewGroup parent) {
        return inflater.inflate(id, parent, false);
    }
}

Related Tutorials