4

well, I'm trying to get my listview height after I change it data, but it always return the prior height, not the actual.

So when I set the setadapter, and it get the old value. e.g:

 ActualHeight = 100
 Change data (filter) -> NewHeight = 60
 ListView.GetHeight still returns 100.

 Again

 ActualHeight = 60
 Change data (filter) -> NewHeight = 20
 ListView.GetHeight still returns 60.

The code i'm using is it:

                    int width, height = 0;

                    EditText edt_search;
                    ListView lv_marca;

                    List<Marca>  list_marca        = new ArrayList<Marca>();
                    List<Marca>  list_marca_search = new ArrayList<Marca>();

                    String text = edt_search.getText().toString(); 


                    list_marca_search.clear();

                    if(text.startsWith(".")){
                        text = text.replace(".", "");
                        for (Marca m : list_marca) {
                            if (String.valueOf(m.getCd_marca()).equals(text)){
                                list_marca_search.add(m);
                            }
                        }
                    } else {
                        for (Marca m : lista_marca) {
                            if (m.getDs_marca().contains(text)){
                                list_marca_search.add(m);
                            }
                        }
                    }

                    ArrayAdapter<Marca> adapter_marca = new CustomAdapter_Marca(MyDialog.this, R.layout.layout_consulta_estoque_marca_lista, list_marca_search);

                    lv_marca.setAdapter(adapter_marca);

                    int height_window  = getWindowManager().getDefaultDisplay().getHeight();
                    height             = lv_marca.getHeight() + getSupportActionBar().getHeight();

                    if (height >= height_window) {
                        height = (int) (height_window * 0.95);
                    }

                    getWindow().setLayout(width, height);
William Da Silva
  • 703
  • 8
  • 19
Cechinel
  • 677
  • 2
  • 8
  • 26

2 Answers2

13

Ok, I found the solution.

When I change the SetAdapter in my ListView I get the measure height that means one line, and multiply by the line numbers plus the divider height by the line numbers too.

Here is a example how I did, I don't know if is the better way but it works perfectly ^^:

    ListView lv_marca; 

    lv_marca.setAdapter(adapter_marca);

    int list_height = getListViewHeight(lv_marca);



    private int getListViewHeight(ListView list) {
          ListAdapter adapter = list.getAdapter();

          int listviewHeight = 0;

          list.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), 
                       MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

          listviewHeight = list.getMeasuredHeight() * adapter.getCount() + (adapter.getCount() * list.getDividerHeight());

          return listviewHeight;
    }
Bandjalong
  • 353
  • 1
  • 3
  • 14
Cechinel
  • 677
  • 2
  • 8
  • 26
  • your code is not running. please do not mark an unapproriate answer as solution. deletion recommended – user1324936 Apr 28 '13 at 13:36
  • @user1324936 Before recommending deletion you should try harder: It works very well. – AlexS Jun 06 '13 at 07:43
  • @Cechinel U r code woking in 2.3 but in 4.2 &4.1 i m getting different height Please give me Solution. – Rohan Pawar Aug 17 '13 at 11:09
  • Should probably calculate `ListItem` height sum, not `ListView` height n times. See http://stackoverflow.com/a/11967081/8524 for better solution. – Diederik Nov 25 '13 at 10:48
  • this is inappropriate answer. it gives height more than the actual height of the content – Harshil Sep 29 '17 at 06:49
1

You will have to add an OnHierarchyChangeListener to your listview so that you can get the value of new height of the listview when child items are added to or removed from the list :

lv_marca.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() 
{
    @Override
    public void onChildViewRemoved(View parent, View child) 
    {
        // This will give you the height when your new adapter contains 
        // lesser number of items than the previous one
        int heightAfterItemRemoved=parent.getHeight();
        Log.i("list_view_height_change","Height after removing item : "+heightAfterItemRemoved);
    }

    @Override
    public void onChildViewAdded(View parent, View child) 
    {
        // This will give you the height when your new adapter contains 
        // greater number of items than the previous one
        int heightAfterItemAdded=parent.getHeight();
        Log.i("list_view_height_change","Height after adding item : "+heightAfterItemAdded);                
    }
});

There is a catch - this will only work if your list view's 'layout_height' property is set to 'wrap_content'. In this case, the height of the list view increases or decreases as items are added or removed, respectively. The list view's height reaches the maximum value when the list view is completely filled. If you add more items to the list view, it will overflow and be scrollable, but the height value will not change, as it is already occupying the maximum permissible height within its parent view.

If you set 'layout_height' to 'fill_parent' or 'match_parent', the list view will occupy the maximum permissible height within its parent view from the very start and hence, removing or adding items to it will not result in any change in its height.

sandyiscool
  • 561
  • 2
  • 9