android - Programmatically add Linear layout with five buttons placed horizontally with same size in both landscape and portrait -


i have programmatically creating linear layout 5 buttons fixed size, when change orientation, buttons stretching in lanscape

here code:

linearlayout button_layout = new linearlayout(this);     button_layout.setlayoutparams(new linearlayout.layoutparams(layoutparams.fill_parent, 100));     button_layout.setorientation(linearlayout.horizontal);     button_layout.setbackgroundcolor(color.parsecolor("#dce1dc"));  framelayout layout = new framelayout(this);     layout.setlayoutparams(new linearlayout.layoutparams(20, 80, 1));     layoutparams params = (layoutparams) layout.getlayoutparams();     params.setmargins(30, 0, 30, 0);      button = new button(this);     a.setlayoutparams(params);     a.settext("a");     button_layout.addview(a);     button b = new button(this);     b.setlayoutparams(params);     b.settext("b");     button_layout.addview(b);     button c = new button(this);     c.setlayoutparams(params);     c.settext("c");     button_layout.addview(c);     button d = new button(this);     d.setlayoutparams(params);     d.settext("d");     button_layout.addview(d);     button e = new button(this);     e.setlayoutparams(params);     e.settext("e");     button_layout.addview(e);     linearlayout.addview(button_layout); //added button layout.       linearlayout bottombarlayout = new linearlayout(this);     bottombarlayout.setlayoutparams(new linearlayout.layoutparams(layoutparams.fill_parent, 50));     bottombarlayout.setorientation(linearlayout.horizontal);     bottombarlayout.setbackgroundcolor(color.parsecolor("#8bc66d"));     linearlayout.addview(bottombarlayout); //added bottombar layout . 

below screen shots:

i want buttons square in both orientations equal spaces, how achieve this?

if understood correctly, trying achieve:

landscape

means, want have space between button , wrapping layout such can set background in , button isn't stretched. and, need 5 buttons have same size in portrait , landscape view.

i took your code , made few modifications: (i use methods here more clean code)

public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      linearlayout linearlayout = (linearlayout) findviewbyid(r.id.layout_root);      linearlayout button_layout = new linearlayout(this);     button_layout.setlayoutparams(new linearlayout.layoutparams(layoutparams.fill_parent, 100));     button_layout.setorientation(linearlayout.horizontal);     button_layout.setbackgroundcolor(color.parsecolor("#dce1dc"));      button_layout.addview(createbutton("a"));     button_layout.addview(createbutton("b"));     button_layout.addview(createbutton("c"));     button_layout.addview(createbutton("d"));     button_layout.addview(createbutton("e"));      linearlayout.addview(button_layout); }  /**  * create 1 button view  *   * @param text  * @return  */ private linearlayout createbutton(string text) {     // wrapping button layout. layout takes 20% parent layout     linearlayout onebuttonlayout = new linearlayout(this);     layoutparams params = new linearlayout.layoutparams(layoutparams.fill_parent, layoutparams.wrap_content, 0.2f);     onebuttonlayout.setlayoutparams(params);     params.setmargins(10, 0, 10, 0);     onebuttonlayout.setgravity(gravity.center);      // button     button button = new button(this);     button.setlayoutparams(new linearlayout.layoutparams(80, 80));     button.settext(text);     onebuttonlayout.addview(button);      return onebuttonlayout; } 

the image above shows how looks in landscape view , how looks in portrait view:

portrait

hope solves problem.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -