android - How to use weights to arrange Gridview Items programmatically with TableLayout -


i working on suduko solver app. want layout have 9x9 textview in 9 gridviews. want put below them 2 buttons (solve, clear). have made not work small devices (can`t see buttons , grid view turned scrollable not fit screen)

here how add textviews gridview

gridview = (gridview) findviewbyid(r.id.gridview1); string[] arrayempty = new string[] {"", "", "", "", "", "", "", "", ""}; arraylist<string> listempty = new arraylist<string>(arrays.aslist(arrayempty)); gridview.setadapter(new arrayadapter<string>(this,r.layout.list_item,list)); 

here layout.xml

<?xml version="1.0" encoding="utf-8"?> 

<tablerow android:id="@+id/tablerow1"     android:layout_width="match_parent"     android:background="#999999"     android:layout_weight="1"     android:layout_height="match_parent">      <gridview     android:id="@+id/gridview1"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" >     </gridview>      <gridview     android:id="@+id/gridview2"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" > </gridview>  <gridview     android:id="@+id/gridview3"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" > </gridview> 

<tablerow android:id="@+id/tablerow2"     android:layout_width="match_parent"     android:background="#999999"     android:layout_weight="1"     android:layout_height="match_parent">      <gridview     android:id="@+id/gridview4"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" >     </gridview>      <gridview     android:id="@+id/gridview5"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" > </gridview>  <gridview     android:id="@+id/gridview6"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" > </gridview> 

<tablerow android:id="@+id/tablerow3"     android:layout_width="match_parent"     android:background="#999999"     android:layout_height="match_parent"     android:layout_weight="1"     android:layout_marginbottom="15dp">      <gridview     android:id="@+id/gridview7"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" >     </gridview>      <gridview     android:id="@+id/gridview8"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" > </gridview>  <gridview     android:id="@+id/gridview9"     android:clickable="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="2dp"     android:background="#5c5c5c"     android:layout_weight="1"     android:numcolumns="3" > </gridview> 

<tablerow android:layout_weight="1" android:layout_height="match_parent">     <button         android:id="@+id/solve_button"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_margin="5dp"         android:layout_weight="1"         android:text="@string/solve" />     <button         android:id="@+id/clear_button"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_margin="5dp"         android:layout_weight="1"         android:text="@string/_clear" /> </tablerow> 

and screenshot of had enter image description here

but need layout devices. maybe should use weights, don`t know how

enter image description here

here's trick: uniform table or grid, don't need tableview or gridview @ all. put inside linearlayout size=0 , layoutweight=1. size=0 seems counter-intuitive, combined layoutweight=1, has effect of giving child widgets exact same size, regardless of contents.

so this:

<!-- outer container; vertical layout fills screen --> <linearlayout     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >   <!-- first row -->   <linearlayout       android:orientation="horizontal"       android:layout_width="fill_parent"       android:layout_height="0dp"       android:layout_weight="1">     <!-- contents of first row -->     <!-- first item in first row -->     <view         android:layout_width="0dp"         android:layout_height="fill_parent"         android:layout_weight="1">     <!-- second item in first row -->     <view         android:layout_width="0dp"         android:layout_height="fill_parent"         android:layout_weight="1">     <!-- third item in first row -->     <view         android:layout_width="0dp"         android:layout_height="fill_parent"         android:layout_weight="1">   </linearlayout>   <!-- second row -->   <linearlayout       android:orientation="horizontal"       android:layout_width="fill_parent"       android:layout_height="0dp"       android:layout_weight="1">     <!-- contents of second row. -->     <!-- etc -->   </linearlayout>   <!-- third row -->   <linearlayout       android:orientation="horizontal"       android:layout_width="fill_parent"       android:layout_height="0dp"       android:layout_weight="1">     <!-- contents of third row -->     <!-- etc -->   </linearlayout>   <button android:id="@+id/solvebutton       android:layout_width="fill_parent"       android:layout_width="wrap_content" />   <button android:id="@+id/clearbutton       android:layout_width="fill_parent"       android:layout_width="wrap_content" /> </linearlayout> 

Comments