Manikanta Reddy
Manikanta Reddy

Reputation: 641

how to increase the listview height dynamically in android?

i want to increase the listview height i am using this method

 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();
        }

this is my listview

<ListView
                android:id="@+id/lvItems"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="#3A9ED4"
                android:dividerHeight="2px" />

but here my problem the height is not increasing based on items it occupies full its shows some empty space can any one suggest me guys...

Upvotes: 1

Views: 3208

Answers (5)

Rahul Ravindran
Rahul Ravindran

Reputation: 304

Try This code I am certain this will work:

1) Create a new class named LVHelper and insert the code below:

public class LVHelper {

// Created By Rahul Ravindran 20/07/17
// This class increases listview's height as and when more item is added

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);
}

2) after setting your list view's adapter add this line of code:

myListView.setAdapter(#Your Adapter Here#);
LVHelper.getListViewSize(myListView);

Upvotes: 0

Jackson
Jackson

Reputation: 81

@Rajesh answer is good just make sure you call the method after your listview has been loaded with its data like below.

setListViewHeightBasedOnChildren(yourlistView);

Upvotes: 0

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

Use this method:

public void setListViewHeightBasedOnChildren(ListView listView) {
     ListAdapter mAdapter = listView.getAdapter();
     int totalHeight = 0;
     for (int i = 0; i < mAdapter.getCount(); i++) {
         View mView = mAdapter.getView(i, null, listView);
         mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
         totalHeight += mView.getMeasuredHeight();
     }
     ViewGroup.LayoutParams params = listView.getLayoutParams();
     params.height = totalHeight + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
     listView.setLayoutParams(params);
     listView.requestLayout();
}

I hope it helps!

Upvotes: 1

Meenaxi
Meenaxi

Reputation: 567

you can set height according to your list items.

int itemcount=listAdapter.getCount();
 ViewGroup.LayoutParams params = listview.getLayoutParams();
 params.height =(itemcount*60);
 listview.setLayoutParams(params);
 listview.requestLayout();

and in layout.xml set listview height to 0dp

  android:layout_height="0dp"

Upvotes: 1

user3902384
user3902384

Reputation:

try this, Use Child based on listview. setListViewHeightBasedOnChildren() this will set your listview child based height.

public class Utils {

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    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);
    listView.requestLayout();
}



}

Upvotes: 1

Related Questions