Programmatically Set Layout Params in Android v7.0 vs. Android 5.0 -
background
in android app, add, remove, , manipulate elements of rows in table programmatically. need set layout parameters of elements in tablerow() upon these table manipulation events.
problem
after migrating android 5.0 android 7.0 , running app on different tablet, table row's not reflect layout param's defined.
to clarify:
configuration 1:
- hardware: samsung galaxy tab4
- android version: 5.0.2
- result: table row's formatted correctly
configuration 2
hardware: samsung galaxy tab s2
android version: 7.0
result: table row's not formatted
code sample
//define layout parameters rowlayoutparams = new tablerow.layoutparams(tablerow.layoutparams.match_parent, tablerow.layoutparams.wrap_content); tablelayoutparams = new tablelayout.layoutparams(tablelayout.layoutparams.match_parent, tablelayout.layoutparams.wrap_content); edittextlayoutparams = new tablerow.layoutparams(100, 40); tvlayoutparams = new tablerow.layoutparams(tablerow.layoutparams.wrap_content, tablerow.layoutparams.wrap_content); //create new table tow tablerow = new tablerow(getactivity()); tablerow.setlayoutparams(rowlayoutparams); //define margins int etmargin1 = (int)getactivity().getresources().getdimension(r.dimen.etmargin1); int etmargin2 = (int)getactivity().getresources().getdimension(r.dimen.etmargin2); int tvmargin1 = (int)getactivity().getresources().getdimension(r.dimen.tvmargin1); int tvmargin2 = (int)getactivity().getresources().getdimension(r.dimen.tvmargin2); if(editable) { //create edit texts edittext exampleet = new edittext(getactivity()); edittextlayoutparams.setmargins(etmargin1,5,etmargin2,5); exampleet.setlayoutparams(edittextlayoutparams); tablerow.setlayoutparams(rowlayoutparams); tablerow.addview(exampleet); } else { //create text views textview exampletv = new textview(getactivity()); //set layout params tvlayoutparams.setmargins(tvmargin1,5,tvmargin2,5); exampletv.setlayoutparams(tvlayoutparams); tablerow.setlayoutparams(rowlayoutparams); tablerow.addview(exampletv); }
further specification
note in code sample edit text's vs. text views. edit text elements not formatted properly. text view margins format expected.
this ended being 2 part solution revolved around different screen sizes of tablets. android version not factor.
part 1 define row layout width , height parameters in terms of density independent pixels rather hard-coded values
int etheight = (int)getactivity().getresources().getdimension(r.dimen.etheight); int etwidth = (int)getactivity().getresources().getdimension(r.dimen.etwidth); edittextlayoutparams = new tablerow.layoutparams(etwidth, etheight);
where in dimens.xml
<dimen name="etheight">40dp</dimen> <dimen name="etwidth">100dp</dimen>
part 2 change table width dimension referenced xml table definition in layout file. margins in fact formatting correctly, table large screen , created illusion elements packed left side of table.
the question whether design layout smaller tablet , waste screen space on larger tablet, or create multiple layout/dimension files support different tablet sizes. but, topic different thread.
Comments
Post a Comment