Code example for TableLayout

Methods: removeAllViewsaddViewsetColumnStretchable

0
    /** 
     * Clears the rows and columns of the table. These will be reconstructed 
     * with fresh data the next time {@link #roundCountChanged} is invoked. 
     */ 
    public void clear() { 
        runningScoresTable.removeAllViews();
        currentScoresTable.removeAllViews();
 
        runningScoresHeader = new TableRow(context);
        runningScoresTable.addView(runningScoresHeader);
 
        TableRow currentScoresHeader = new TableRow(context);
        TextView total = newTextView(-1);
        total.setText(pad("Σ"));
        currentScoresHeader.addView(total);
        currentScoresTable.addView(currentScoresHeader);
 
        runningScoresRows = new TableRow[model.playerCount()];
        playerTotals = new TextView[model.playerCount()];
 
        TableLayout.LayoutParams WRAP_PARAMS = new TableLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 
        for (int p = 0; p < model.playerCount(); p++) {
            TableRow playerRunningScores = new TableRow(context);
            runningScoresRows[p] = playerRunningScores;
            runningScoresTable.addView(playerRunningScores);
 
            TableRow currentScoresRow = new TableRow(context);
 
            TextView playerTotal = newTextView(p);
            playerTotal.setText(totalString(p));
            playerTotals[p] = playerTotal;
            playerTotal.setTypeface(null, Typeface.BOLD);
            currentScoresRow.addView(playerTotal);
 
            TextView playerName = newTextView(p);
            playerName.setGravity(Gravity.LEFT);
            playerName.setText(model.playerName(p));
            currentScoresRow.addView(playerName);
 
            currentScoresTable.addView(currentScoresRow, WRAP_PARAMS);
        } 
 
        runningScoresTable.setColumnStretchable(0, true);
    } 
 
    public void scoreChanged(int player, int round) {
        TextView roundScore = (TextView) runningScoresRows[player].getChildAt(round);
        roundScore.setText(getScore(player, round));