listview - view.getMeasuredHeight() always returns same height android -


i have listview having comment listing different height of each row according comment length. want give height listview programmatically. tried height of each row, every time same height each row.. each row has different size according content. have used following code:

 listadapter listadapter = listview.getadapter();     if (listadapter != null) {          int numberofitems = listadapter.getcount();          // total height of items.         int totalitemsheight = 0;         (int itempos = 0; itempos < numberofitems; itempos++) {             view item = listadapter.getview(itempos, null, listview);             item.measure(0, view.measurespec.unspecified);              totalitemsheight += item.getmeasuredheight();             log.e("height", "" + item.getmeasuredheight());         }          // total height of 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();       } 

you have wait laid out , measured.

if want force item wraps content can call measure this:

item.measure(measurespec.makemeasurespec(maxwidth, measurespec.at_most),     measurespec.makemeasurespec(maxheight, measurespec.at_most)); 

before calling getmeasuredheight. remember though, return pixels not dp may need conversion.


Comments

Popular posts from this blog

php - Embed BBCode within itself not working -

three.js - how to load gltf in three JS -

sql server - T-SQL: Access DB objects in same schema without qualifying? -