Resizing dynamic TableLayout in android -
i have created dynamic tablelayout in android. problem is, there unnecessary gap in column. i'd remove it, i've been changing code few times results astonishing. here best can @ moment.
as can see, there gap in header , in row if row consisting 2+ lines. want make first column (which items) have longest width while other column wraps content header.
here xml
<tablelayout android:id="@+id/tabledescription" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchcolumns="*"> <tablerow android:layout_width="match_parent" android:layout_height="wrap_content" android:weightsum="4"> <textview android:layout_width="@dimen/size0" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/cell_shape" android:padding="@dimen/size5" android:text="@string/items" /> <textview android:layout_width="@dimen/size0" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/cell_shape" android:padding="@dimen/size5" android:text="@string/quantity" /> <textview android:layout_width="@dimen/size0" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/cell_shape" android:padding="@dimen/size5" android:text="@string/unitprice" /> <textview android:layout_width="@dimen/size0" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/cell_shape" android:padding="@dimen/size5" android:text="@string/total" /> </tablerow> </tablelayout>
and here code make dynamic row
private void init() { // array item string[] valuesitem = new string[] { "room asd", "room qwewq", "room kadskadskl", "room oeqwoeqwo", "room uuawuwauaw" }; final arraylist<string> listitem = new arraylist<string>(); (int = 0; < valuesitem.length; ++i) { listitem.add(valuesitem[i]); } // array quantity string[] valuesquantity = new string[] { "1", "1", "2", "1", "2" }; final arraylist<string> listquantity = new arraylist<string>(); (int = 0; < valuesquantity.length; ++i) { listquantity.add(valuesquantity[i]); } // array price string[] valuesprice = new string[] { "10", "10", "20", "50", "30" }; final arraylist<string> listprice = new arraylist<string>(); (int = 0; < valuesprice.length; ++i) { listprice.add(valuesprice[i]); } // array total string[] valuestotal = new string[] { "10", "10", "40", "50", "60" }; final arraylist<string> listtotal = new arraylist<string>(); (int = 0; < valuestotal.length; ++i) { listtotal.add(valuestotal[i]); } (int = 0; <valuesitem.length; i++) { tablerow row= new tablerow(getactivity()); tablerow.layoutparams lp = new tablerow.layoutparams(tablerow.layoutparams.match_parent, tablerow.layoutparams.match_parent); row.setlayoutparams(lp); textview item = new textview(getactivity()); textview quantity = new textview(getactivity()); textview price = new textview(getactivity()); textview total = new textview(getactivity()); item.settext(valuesitem[i]); quantity.settext(valuesquantity[i]); price.settext(valuesprice[i]); total.settext(valuestotal[i]); item.setpadding(getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5), (getactivity().getresources().getdimensionpixelsize(r.dimen.size5)), getactivity().getresources().getdimensionpixelsize(r.dimen.size5)); quantity.setpadding(getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5)); price.setpadding(getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5)); total.setpadding(getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5), getactivity().getresources().getdimensionpixelsize(r.dimen.size5)); item.setwidth(getactivity().getresources().getdimensionpixelsize(r.dimen.size0)); quantity.setwidth(getactivity().getresources().getdimensionpixelsize(r.dimen.size0)); price.setwidth(getactivity().getresources().getdimensionpixelsize(r.dimen.size0)); total.setwidth(getactivity().getresources().getdimensionpixelsize(r.dimen.size0)); item.setbackgroundresource(r.drawable.cell_shape); quantity.setbackgroundresource(r.drawable.cell_shape); price.setbackgroundresource(r.drawable.cell_shape); total.setbackgroundresource(r.drawable.cell_shape); row.addview(item); row.addview(quantity); row.addview(price); row.addview(total); tabledynamic.addview(row); } }
sorry if it's bit messy. thanks!
solved configuring layoutparams each textview
lp = new tablerow.layoutparams(0, tablerow.layoutparams.match_parent, 5f); item.setlayoutparams(lp); lp = new tablerow.layoutparams(0, tablerow.layoutparams.match_parent, 2f); quantity.setlayoutparams(lp); lp = new tablerow.layoutparams(0, tablerow.layoutparams.match_parent, 1.5f); price.setlayoutparams(lp); lp = new tablerow.layoutparams(0, tablerow.layoutparams.match_parent, 1.5f); total.setlayoutparams(lp);
Comments
Post a Comment