Beppe
Beppe

Reputation: 201

How to add objects programmatically in table rows FROM Right to Left

I've a Table Layout and a Table Row in it. I can add buttons through code on it by the below lines:

for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (int row = 0; row < 3; row++) {
TableRow currentTableRow = getTableRow(row);
                for (int column = 0; column < 7; column++) {
                    Button newGuessButton = (Button)inflater.inflate(R.layout.my_button, currentTableRow, false);
                    String myName = new String(String.valueOf((row * 7) + column + 1));
                    newGuessButton.setText(myName);
                    currentTableRow.addView(newGuessButton);
                }
            }
 private TableRow getTableRow(int row) {
    return (TableRow) buttonTableLayout.getChildAt(row);
}

but I want to add buttons from RIGHT to LEFT. not just align from right, like:

enter image description here

thanks.

Upvotes: 0

Views: 218

Answers (1)

Rami
Rami

Reputation: 7929

You can change your TableRow gravity with currentTableRow.setGravity(Gravity.RIGHT)

Then reverse your for-loop, to keep the items order:

for (int column = 6; column >= 0; column--) {
     Button newGuessButton = (Button)inflater.inflate(R.layout.my_button, currentTableRow, false);
     String myName = new String(String.valueOf((row * 7) + column + 1));
     newGuessButton.setText(myName);
     currentTableRow.addView(newGuessButton);
}

Upvotes: 1

Related Questions