• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Table rows not displayed as expected

Nedals

Lurker
I am attempting to display a table row and, after a delay (call to our server), change the row background. But the row ONLY displays after the delay.

This greatly simplified code to demonstrates the problem. I want the inital 'white' row to display immediately and a 'green' row to display 2 secs later; but both display only after the 2 sec delay.

How do I get this to work the way I want it?

Code:
public class EditLayout extends AndroidTest {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tables);
        ((TextView) findViewById(R.id.pagename)).setText("T A B L E   L A Y O U T");
        getInput();
    }
    
    private void getInput() {
        final EditText dataField = (EditText)findViewById(R.id.data);
        final TableLayout tbl = (TableLayout) findViewById(R.id.tbl);
        
        Button enter = (Button)findViewById(R.id.enterBtn);
        enter.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                String dataIn = tagField.getText().toString();
                dataField.setText("");        // Clear the input field
                
                String[] rowText = {dataIn,"white"};
                addRow(tbl, rowText);    
            
                try { Thread.sleep(2000); } catch (Exception e) {}    
                
                String[] response = {dataIn,"green"};
                addRow(tbl, response);    
            }
        });
    }
    
    private void addRow(TableLayout tbl, String[] rowText) {
        TableRow rowData = buildRow(rowText);
        tbl.addView(rowData);
    }

    private TableRow buildRow(String[] rowData) {    
        int rowColor = (rowData[1].equals("green")) ? 0x8800FF00 : 0xFFFFFFFF;
    
        TableRow rx = new TableRow(EditLayout.this);    
        TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(-1, -2);
        rowParams.setMargins(0, 0, 0, 2);        //lt, tp, rt, bt
        rx.setLayoutParams(rowParams);
        rx.setPadding(5, 0, 0, 0);                //lt, tp, rt, bt;
        rx.setBackgroundColor(rowColor);        //0xFFFFFFFF);
    
        TextView c0 = new TextView(EditLayout.this);
        c0.setLayoutParams(new LayoutParams(150, -2));
        c0.setTextSize(18);
        c0.setTextColor(0xFF000000);
        c0.setText(rowData[0]);
        rx.addView(c0);
        return rx;
    }    
}
 
Back
Top Bottom