java - How to programmatically add buttons to a layout that don't stack on top of each other? -
i'm trying make layout populates buttons defined in jsonobject in file buttons.txt. generating buttons file works problem buttons stacking on top of each other. i've tried adding different layoutparams buttons params define relation layout seem work, , none alter relationship other child views. how can buttons "flow" of layout?
private void createbuttons(relativelayout layout, jsonobject jo, relativelayout.layoutparams lp){ iterator<?> keys = jo.keys(); while( keys.hasnext() ) { string key = (string)keys.next(); button newbutton = new button(this); newbutton.settext(key); layout.addview(newbutton, lp); jsonobject subcat = null; try { subcat = jo.getjsonobject(key); } catch (jsonexception e) { subcat = null; } if ( subcat instanceof jsonobject ) { createbuttons(layout, (jsonobject) subcat, lp); } } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); relativelayout rl = (relativelayout)findviewbyid(r.id.main_layout); relativelayout.layoutparams lp = new relativelayout.layoutparams(111, 111); lp.addrule(relativelayout.center_horizontal); lp.addrule(relativelayout.right_of); assetmanager = getassets(); string json = null; try { inputstream = am.open("buttons.txt"); json = readfully(is, "utf-8"); } catch (ioexception e) { e.printstacktrace(); } jsonobject jo = null; try { jo = (jsonobject) new jsontokener(json).nextvalue(); } catch (jsonexception e) { e.printstacktrace(); } createbuttons(rl, jo, lp); }
you're giving buttons same layout params in relative layout, stack them. try add them linear layout add them sequentially. can apply centering linear layouts also, won't stack.
Comments
Post a Comment