Note that there are some explanatory texts on larger screens.

plurals
  1. POAlgorithm for looping three Buttons in a TableRow
    primarykey
    data
    text
    <p><code>int numberofbutton=x;</code> will be provided.</p> <p>I tried to loop but I failed because <code>numberofbutton</code> has to be divisible by 3. I can't write algorithm.</p> <p>Based on <code>numberofbutton</code>, we have to loop three buttons in a TableRow programmatically.<br/> I mean, if numberofbutton is <strong>4</strong>,</p> <pre><code>TableRow[0]-&gt;Button Button Button TableRow[1]-&gt;Button </code></pre> <p>If it's <strong>3</strong>,</p> <pre><code>TableRow[0]-&gt;Button Button Button </code></pre> <p><strong>UPDATE:</strong> Based on ZouZou's answer, code is below but unsolved because tr[] is undefined:<br/></p> <pre><code>int bn = 9; if (bn % 3 == 0) { TableRow[] tr = new TableRow[bn / 3]; for (int i = 1; i &lt;=(bn / 3); i++) { tr[i] = new TableRow(this); } } else { TableRow[] tr = new TableRow[(bn / 3) + 1]; for (int i = 1; i &lt;=(bn / 3) + 1; i++) { tr[i] = new TableRow(this); } } Button[] b=new Button[bn]; for(int i=1;i&lt;=bn;i++){ b[i]=new Button(this); } int index = -1; for (int i = 0; i &lt; bn; i++) { if(i%3==0){ index+=1; tr[index].addView(b[i]); }else{ tr[index].addView(b[i]); } } </code></pre> <p><br/> <strong>UPDATE</strong>:</p> <p>Full code but why doesn't it work?:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TableLayout tL = (TableLayout) findViewById(R.id.table); int bn = 9; TableRow[] tr = null; if (bn % 3 == 0) { tr = new TableRow[bn / 3]; for (int i = 1; i &lt;= (bn / 3); i++) { tr[i] = new TableRow(this); } } else { tr = new TableRow[(bn / 3) + 1]; for (int i = 1; i &lt;= (bn / 3) + 1; i++) { tr[i] = new TableRow(this); } } Button[] b=new Button[bn]; for(int i=1;i&lt;=bn;i++){ b[i]=new Button(this); } int index = -1; for (int i = 0; i &lt; bn; i++) { if(i%3==0){ index+=1; tr[index].addView(b[i]); tL.addView(tr[index]); }else{ tr[index].addView(b[i]); } } } </code></pre> <p><br/></p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" &gt; &lt;TableLayout android:id="@+id/table" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="59dp" android:layout_marginTop="44dp" &gt; &lt;/TableLayout&gt; &lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload