title: How to have a GridView that adapts its height when items are added
body: <p>I am trying to display a dynamically-growing list of strings with a checkbox in a GridView, which is itself in a TableLayout.</p>
  
  <p>I can display these "checkboxed" strings fine in a row. My problem occurs when I let the user dynamically add new strings in the GridView.</p>
  
  <p>I created a custom adapter that receives the list of strings. Say we have n strings. The adapter returns 'n + 1' for the items count; in getView, it returns:</p>
  
  <ul>
  <li>a View with a LinearLayout, itself having a CheckBox and an EditText for the first n items,</li>
  <li>a LinearLayout with a simple button, with a '+' caption for the 'n + 1'th item.</li>
  </ul>
  
  <p>So far so good. When the '+' button is clicked, I add an empty string to the list of strings and call notifyDataSetChanged in the adapter. </p>
  
  <p>The GridView redraws itself with one more item. BUT it keeps its original height and creates a vertical scrollbar. I'd like the GridView to expand its height (i.e. take up more space on screen and show all items).</p>
  
  <p>I've tried to change the screen to a vertical LinearLayout instead of a TableLayout, but the results is the same.</p>
  
  <p>Here is the layout of my screen:</p>
  
  <pre><code>&lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="none"&gt;
  &lt;LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      &gt;
  
      &lt;!-- other lines omitted --&gt;
  
      &lt;LinearLayout android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" &gt;
          &lt;TextView
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
              android:text="@string/wine_rack_explanation"
              android:layout_gravity="center_vertical" /&gt;
            &lt;GridView
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content"
              android:id="@+id/gvRack"
              android:columnWidth="90dp"    
              android:numColumns="auto_fit" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;!-- other lines omitted --&gt;
  
  &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>The checkbox + string item from the adapter is defined like this:</p>
  
  <pre><code>&lt;LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"&gt;
      &lt;CheckBox android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/cbCoordinate"
                android:checked="true"
                /&gt;
      &lt;EditText android:id="@+id/txtCoordinate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The '+' button item is defined like this:</p>
  
  <pre><code>&lt;LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"&gt;
  
      &lt;Button android:id="@+id/btnAddBottle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/add_bottle_caption" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I've tried to call invalidate or invalideViews on the GridView after an item is added. Also called invalidate on the TableLayout and TableRow (in the previous layout of the screen). No success.</p>
  
  <p>Any idea why the GridView refuses to extend its height ?</p>
  
  <p>(Note that I am completely open to using an other viewgroup than the GridView)</p>