8

I need to change the height of a ListView dynamically in my app.

Is there any way to do that?

Pang
  • 9,564
  • 146
  • 81
  • 122
surendra
  • 2,217
  • 7
  • 34
  • 42

7 Answers7

30

This piece of code helped me to achieve dynamic listview height.

import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.ListAdapter;
import android.widget.ListView;

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

In the main Activity use this Utility class to change the listview height.

 phraseListView=(ListView)findViewById(R.id.phrase_listview);
 phraseAdapter=new PhraseListAdapter(this);
 phraseListView.setAdapter(phraseAdapter);
 Utility.setListViewHeightBasedOnChildren(phraseListView);

Taken from here

eimie
  • 93
  • 9
Rafael
  • 6,091
  • 5
  • 54
  • 79
  • Perfection Simplified. Thank you – kholofelo Maloma Nov 15 '16 at 20:08
  • 1
    Thanks, this worked for me too. Actually i am using Listview inside scrollview in one big form. That's why i have to use this code other wrap wrap_content value in listview height property works best. Anyways nice utility good job :) – Ahesanali Suthar May 05 '17 at 12:25
8

for change height and width dynamically so, try this

RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams((int)(width),(int)(height);
        listView.setLayoutParams(mParam);

you can also use LayoutParams.FILL_PARENT or LayoutParams.WRAP_CONTENT instead of height & width

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • 3
    the hereover solution give me a casting error, changing to LinearLayout works great. LinearLayout.LayoutParams mParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,myList.size()*100); listViewPhone.setLayoutParams(mParam); – Dr.Luiji Feb 22 '13 at 15:04
  • @Dr.Luiji I have using something like this `RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, item.getRoutes().size() * 200);` . But I am unable to see some item from the list. Any suggestion, I want to display whole list item of the list. What can I do ? – Satan Pandeya Oct 20 '16 at 09:32
7

You can use below method to set the height of listview programmatically as per your items:

<ListView
        android:id="@+id/lvMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        />

Method used for setting height of listview:

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);
                float px = 500 * (listView.getResources().getDisplayMetrics().density);
                item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                totalItemsHeight += item.getMeasuredHeight();
            }

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

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

        } else {
            return false;
        }

    }

Then set it like below code:

MenuAdapter menuAdapter = new MenuAdapter(context, R.layout.menu_item);
            lvMenu.setAdapter(menuAdapter);
            setListViewHeightBasedOnItems(lvMenu);

Hope it will help you.

KishuDroid
  • 5,411
  • 4
  • 30
  • 47
2

First thing to keep in mind is that while getting height of listview using mylistview.getmeasureheight, it always gives height according to the textview/View inside the list which was set during initializing of adapter. So we need to declare the adapter as follows:

myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myarraylist){
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = ((TextView) view.findViewById(android.R.id.text1));
            textView.setMinHeight(0); // Min Height
            textView.setMinimumHeight(0); // Min Height
            textView.setHeight(100); // Height
            return view;
        }
    };

Now height is fixed to 100. Note that it's the height of text and not the view itself. So keep it in mind when adding padding. Now set the Adapter:

mylist.setAdapter(myadapter);

Now after every time you add an item to your list call a function as follows:

myarraylist.add(ds.getKey());
myadapter.notifyDataSetChanged();
mylist.setAdapter(myadapter);

setListViewHeightBasedOnItems(mylist);

define the function in same class as:

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);
            float px = 500 * (listView.getResources().getDisplayMetrics().density);
            item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalItemsHeight += item.getMeasuredHeight();
        }

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

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

    } else {
        return false;
    }

}

You can also check if height is set or not correctly by returning true/false. This solution worked for me. Hope this Helps!!!!!

R.A.Y
  • 95
  • 2
  • 5
1

You have to create a new control that extends the listview overwhriting some functions

public class WrapContentListView extends ListView {
    public WrapContentListView(Context context) {
        super(context);
    }

    public WrapContentListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WrapContentListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
        setHeightWrapContent();
    }

    public void setHeightWrapContent() {
        ListAdapter listAdapter = getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, this);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = this.getLayoutParams();

        params.height = totalHeight
                + (this.getDividerHeight() * (listAdapter.getCount() - 1));
        this.setLayoutParams(params);
    }
}

https://github.com/mzlogin/WrapContentListView/blob/master/app/src/main/java/org/mazhuang/wrapcontentlistview/WrapContentListView.java

gureangel
  • 155
  • 1
  • 7
0

Personaly I spent so much time trying to find a solution, turns out all I had to do is to set my listview's height to wrap_content.

Hind
  • 1
-3

use customized listview and give height as wrap_content.

Kakey
  • 3,936
  • 5
  • 29
  • 45