Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get an Android TableLayout to fill the screen?
    text
    copied!<p>I'm battling with Android's awful layout system. I'm trying to get a table to fill the screen (simple right?) but it's ridiculously hard.</p> <p>I got it to work somehow in XML like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"&gt; &lt;TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"&gt; &lt;Button android:text="A" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/&gt; &lt;Button android:text="B" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/&gt; &lt;/TableRow&gt; &lt;TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"&gt; &lt;Button android:text="C" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/&gt; &lt;Button android:text="D" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/&gt; &lt;/TableRow&gt; </code></pre> <p>However I can not get it to work in Java. I've tried a million combinations of the LayoutParams, but nothing ever works. This is the best result I have which only fills the width of the screen, not the height:</p> <pre><code> table = new TableLayout(this); // Java. You suck. TableLayout.LayoutParams lp = new TableLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); table.setLayoutParams(lp); // This line has no effect! WHYYYY?! table.setStretchAllColumns(true); for (int r = 0; r &lt; 2; ++r) { TableRow row = new TableRow(this); for (int c = 0; c &lt; 2; ++c) { Button btn = new Button(this); btn.setText("A"); row.addView(btn); } table.addView(row); } </code></pre> <p>Obviously the Android documentation is no help. Anyone have any ideas?</p>
 

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