title: Scroll to last line of TableLayout within a ScrollView
body: <p>I want to have a dynamic table, with rows added over time as a result of user interaction, using a TableLayout inside a ScrollView. This works fine, but when I want to scroll to the end of the table using <a href="http://developer.android.com/reference/android/widget/ScrollView.html#fullScroll%28int%29" rel="nofollow noreferrer"><code>fullScroll()</code></a>, it always leaves out the last line; that is, it scrolls so that the one before the last one is visible. The last line is visible when scrolling manually, and the scrollbar is correct too.</p>
  
  <p>I'm of course open to suggestions as to how to make a better layout out of this; but I'm specifically interested in understanding why <code>fullScroll()</code> behaves that way. Should I give it a different parameter, or use something else altogether? Or does it do that because the newly added line isn't yet visible somehow? (if so, how can I solve that?) Or did I miss some other obvious thing?</p>
  
  <p>The following code replicates the problem:</p>
  
  <p>TestActivity.java:</p>
  
  <pre><code>package com.example.android.tests;
  
  import java.util.Random;
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ScrollView;
  import android.widget.TableLayout;
  import android.widget.TableRow;
  import android.widget.TextView;
  
  public class TestActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          ((Button) findViewById(R.id.AddRow)).setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Random rnd = new Random();
                  TableRow nr = new TableRow(v.getContext());
                  for (int c=0; c&lt;3; c++) {
                      TextView nv = new TextView(v.getContext());
                      nv.setText(Integer.toString(rnd.nextInt(20)-10)); 
                      nr.addView(nv);
                  }
                  ((TableLayout) findViewById(R.id.Table)).addView(nr);
                  // Scrolls to line before last - why?
                  ((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
              }
          });
  
      }
  }
  </code></pre>
  
  <p>main.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"&gt;
    &lt;Button
      android:text="Add Row"
      android:id="@+id/AddRow"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true" /&gt;
    &lt;ScrollView
      android:id="@+id/TableScroller"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_above="@id/AddRow"
      android:layout_alignParentTop="true" &gt;
      &lt;TableLayout
        android:id="@+id/Table"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1,2" /&gt;
    &lt;/ScrollView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <hr>
  
  <p>Edit: for reference, I implemented <a href="https://stackoverflow.com/users/298575/romain-guy">Romain Guy</a>'s solution as follows:</p>
  
  <p>In TestActivity.java, replace:</p>
  
  <pre><code>            // Scrolls to line before last - why?
              ((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>            // Enqueue the scrolling to happen after the new row has been layout
              ((ScrollView) findViewById(R.id.TableScroller)).post(new Runnable() {
                  public void run() {
                      ((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
                  }
  
              });
  </code></pre>
  
  <p>Which works fine.</p>