Search code examples
javaandroidrowandroid-tablelayout

How to set columns programmatically to have dynamic with in Android?


I have created a table like this:

TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);

tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.LEFT);
for (int j = 0; j < 4; j++) {
    statistics = new TextView[4];
    statistics[i] = new TextView(getApplicationContext());
    statistics[i].setText("Text");
    statistics[i].setGravity(Gravity.LEFT);
    tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);

The result of this code is:

enter image description here

And I want to achieve this:

enter image description here

How is this possible?


Solution

  • With this

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*"
    
       <TableRow 
            android:layout_weight="1"
            android:gravity="center">
    
            <ImageButton
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:scaleType="center"
                android:src="@drawable/ic_1" 
                android:background="@null" />
    
            <ImageButton
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:scaleType="center"
                android:src="@drawable/ic_2" 
                android:background="@null"/>
    
            <ImageButton
                android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:scaleType="center"
                android:src="@drawable/ic_3" 
                android:background="@null" />
        </TableRow>
     </TableLayout>
    

    The three button appears aligned. I think you need gravity="center"and/or android:stretchColumns="*" in your code.

    UPDATE

    Try with this:

            TableLayout table = new TableLayout(getApplicationContext());
            table.setColumnStretchable(1, true);
            table.setColumnStretchable(2, true);
            table.setStretchAllColumns(true);
    
            tableRow = new TableRow[20];
            tableRow[i] = new TableRow(getApplicationContext());
            tableRow[i].setGravity(Gravity.CENTER);
            for (int j = 0; j < 4; j++) {
                statistics = new TextView[4];
                statistics[i] = new TextView(getApplicationContext());
                statistics[i].setText("Text");
                statistics[i].setGravity(Gravity.LEFT);
                tableRow[i].addView(statistics[i]);
            }
            table.addView(tableRow[i]);
    

    UPDATE

    I did a mock of your problem and use this

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            TableLayout table = new TableLayout(getApplicationContext());
            table.setStretchAllColumns(true);
    
            for (int i = 0; i<20; i++) {
    
                TableRow[] tableRow = new TableRow[20];
                tableRow[i] = new TableRow(getApplicationContext());
                tableRow[i].setGravity(Gravity.CENTER);
    
                TextView pos = new TextView(getApplicationContext());
                pos.setGravity(Gravity.LEFT);
                pos.setText(String.valueOf(i) + ". " + getName(i));
    
                TextView a = new TextView(getApplicationContext());
                a.setGravity(Gravity.LEFT);
                a.setText("2/9");
    
                TextView points = new TextView(getApplicationContext());
                points.setGravity(Gravity.LEFT);
                points.setText("2/9");
    
                tableRow[i].addView(pos);
                tableRow[i].addView(a);
                tableRow[i].addView(points);
    
                table.addView(tableRow[i]);
            }
    
            RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
            container.addView(table);
        }
    
        private String getName(int i) {
    
            if (i == 2) {
                return "Recooooooooooooooord";
            } else if (i == 3) {
                return "Recooooooord";
            }
    
            return "Fran";
        }
    

    The result is like the one you want, you can see the result here