setBackgroundColor causing TableLayout alignment problems
<p>Re-wording per previous request... I am dynamically adding TableLayouts at runtime.  A view rundown looks like this...  LinearLayout->ScrollView->TableLayout->Loop creating TableLayouts and TableRows at runtime.  So basically I am looping and adding the following:</p>

<p>for(i=0.... i++)</p>

<p>Left &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Middle &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Right </p>

<p>Bottom</p>

<p>Now everything gets added correctly, looks correct and scrolls correctly.  However I am attempting to create an onTouch event which changes the background on the touched dynamically created TableLayout.  Inside the onTouch for each dynamically created TableLayout I have a onTouch event that when called does the following:</p>

<pre><code>            tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("OnTouch", "OnTouch Event for " + Integer.toString(v.getId()) + " event:" + Integer.toString(event.getAction()));

                switch(event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("OnTouch", "Changing to GREEN");
                        v.setBackgroundColor(Color.GRAY);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_MOVE:
                        Log.d("OnTouch", "Changing to BLACK");
                        v.setBackgroundColor(Color.BLACK);
                        break;
                }

                return true;
            }
        });
</code></pre>

<p>Whenever it calls the setBackgroundColor it messes up the alignment of every one of the dynamically created TableLayouts and makes them look like this:</p>

<p>LeftMiddleRight</p>

<p>Bottom</p>

<p>The entire code follows.  I hope this is clear now.</p>

<pre><code>public class TableActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    linearLayout.setWeightSum(1);

    ScrollView scrollView = new ScrollView(this);
    scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    TableLayout tableLayoutMaster = new TableLayout(this);
    tableLayoutMaster.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    // &lt;table&gt;
    //      &lt;tr&gt;&lt;td&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;
    //      &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
    // &lt;/table&gt;

    for(int i=0; i&lt;=30; i++)
    {
        TableLayout tableLayoutRow1 = new TableLayout(this);
        tableLayoutRow1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        tableLayoutRow1.setColumnStretchable(2, true);
        //tableLayoutRow1.setBackgroundColor(Color.GREEN);
        TableRow tableRow = new TableRow(this);

        ImageView left_column = new ImageView(this);
        left_column.setImageResource(17301550);
        tableRow.addView(left_column);

        TextView middle_column = new TextView(this);
        middle_column.setText("Left Column:" + Integer.toString(i));
        middle_column.setTypeface(null, Typeface.BOLD);
        middle_column.setGravity(Gravity.LEFT);
        middle_column.setPadding(5, 0, 0, 0);
        tableRow.addView(middle_column);

        TextView right_column = new TextView(this);
        right_column.setText("Right Column:" + Integer.toString(i));
        right_column.setTypeface(null, Typeface.BOLD);
        right_column.setGravity(Gravity.RIGHT);
        right_column.setPadding(0, 0, 5, 0);
        tableRow.addView(right_column);

        tableLayoutRow1.addView(tableRow);
        tableRow = null;

        // now for the bottom row
        TextView bottom_column = new TextView(this);
        bottom_column.setText("Starting activity com.developersinfo.testing.TableActivity on device emulator-5554");
        bottom_column.setTextSize(12f);
        bottom_column.setGravity(Gravity.LEFT);
        bottom_column.setPadding(30, 0, 0, 0);
        tableLayoutRow1.addView(bottom_column);

        tableLayoutRow1.setId(i);
        tableLayoutRow1.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                Log.d("OnFocusChange", "OnFocusChange Event for " + Integer.toString(v.getId()));
            }
        });
        tableLayoutRow1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d("OnClick", "OnClick Event for " + Integer.toString(v.getId()));
            }
        });
        tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("OnTouch", "OnTouch Event for " + Integer.toString(v.getId()) + " event:" + Integer.toString(event.getAction()));

                switch(event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("OnTouch", "Changing to GREEN");
                        v.setBackgroundColor(Color.GRAY);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_MOVE:
                        Log.d("OnTouch", "Changing to BLACK");
                        v.setBackgroundColor(Color.BLACK);
                        break;
                }

                return true;
            }
        });

        // now add to master table
        tableLayoutMaster.addView(tableLayoutRow1);
    }

    scrollView.addView(tableLayoutMaster);
    linearLayout.addView(scrollView);
    setContentView(linearLayout);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
</code></pre>

<p>}</p>

<p>I have tried everything I can think of to keep it from doing this but so far I am unable to explain why or how to correct it.  As long as I do not call the setBackgroundColor inside the onTouch everything scrolls and aligns as expected. </p>
