android - How to Programmatically Add Views to Views -


let's have linearlayout, , want add view it, in program java code. method used this? i'm not asking how it's done in xml, know, rather, how can along lines of this?

(one view).add(another view)

like 1 can in swing.

calling addview correct answer, need little more work.

if create view via constructor (e.g., button mybutton = new button();), you'll need call setlayoutparams on newly constructed view, passing in instance of parent view's layoutparams inner class, before add newly constructed child parent view.

for example, might have following code in oncreate() function assuming linearlayout has id r.id.main:

linearlayout mylayout = findviewbyid(r.id.main);  button mybutton = new button(this); mybutton.setlayoutparams(new linearlayout.layoutparams(                                      linearlayout.layoutparams.match_parent,                                      linearlayout.layoutparams.match_parent));  mylayout.addview(mybutton); 

making sure set layoutparams important. every view needs @ least layout_width , layout_height parameter. getting right inner class important. struggled getting views added tablerow display until figured out wasn't passing instance of tablerow.layoutparams child view's setlayoutparams.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -