java - Android: Nested Linearlayouts will not display a grid of buttons, Matching parent -


i have app under construction. in 1 of sub-menus have need generic display of buttons, , therefor want make activity can display given number of needed buttons.

i have succesfully made happen, programmatically, want total grid of buttons fill entire parent placed in, happens 3/4 of landscape screen. number of buttons varies 16-38.!

i have succesfully made happen other grids of buttons, in xml, weight values, , match_parent values of entries.

when assign buttons or rows match_parent value programatically, occupies entire parent layout, not sharing expect do, though have same weight value of 1.0f

the relevant code follows below. post images well, have not reputation so.

`linearlayout layout = (linearlayout) findviewbyid(r.id.linear_custom_draw); layout.setorientation(linearlayout.vertical);

    int columns = math.min(6, 4+category); //sets number of buttons per row 4-6      (int = 0; < 4+category; i++) {         linearlayout row = new linearlayout(this);         row.setlayoutparams(new android.view.viewgroup.layoutparams(android.view.windowmanager.layoutparams.match_parent, 

android.view.windowmanager.layoutparams.match_parent)); //the line above line fills entirety of linearlayout, though there more entries, unlike xml-defined attempts. row.setorientation(linearlayout.horizontal); row.setweightsum(1.0f); if(i%2 == 0){ row.setbackgroundcolor(getresources().getcolor(r.color.listview_red_backgr_color)); }

        (int j = 0; j < columns; j++) {             int index = (i*columns)+j;             if(formations.size() > index){                 button btntag = new button(this);                 btntag.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content));                 btntag.settext(formations.get(index).getname());                 btntag.settextcolor(getresources().getcolor(r.color.black_overlay));                     btntag.setid(formations.get(index).getid());                 row.addview(btntag);             }         }          layout.addview(row);` 

try use tablelayout. each row enforce entire elements match parent same wights. can control number of buttons each row programatically counter. loop end of counter adding buttons add new table row

tablelayout tbl=new tablelayout(context);//create table tablerow tr=new tablerow(context);//create table row tr.addview(view);//add button instead of view tbl.addview(tr);//add row table 

in xml file

<tablelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/keypad" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:stretchcolumns="*"> <tablerow>     <button android:id="@+id/keypad_1" android:text="@string/_1"></button>     <button android:id="@+id/keypad_2" android:text="@string/_2"></button>     <button android:id="@+id/keypad_3" android:text="@string/_3"></button> </tablerow> </tablelayout> 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -