androidandroid-tablelayout

Android table layout adding single column row that spans table width in java


I am trying to add a single textview row to a table in android the row should merge and center the width of the table

I am able to achieve this using XML

    <TableLayout>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/red_cell_shape"
            android:text="code"
            android:textAlignment="center"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/red_cell_shape"
            android:padding="5dp"
            android:text="desc"
            android:textAlignment="center"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/red_cell_shape"
            android:text="status"
            android:textAlignment="center"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:id="@+id/tremptyrow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/red_cell_shape">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/no_fg_scanned"
            android:padding="5dp"
            android:layout_span="6"
            android:textAlignment="center"
            android:textColor="@color/red" />
    </TableRow>

</TableLayout>

this is what it should look like enter image description here

but I want to do it dynamically in java and tried this but did not work


Solution

  • You can implement the above layout programmatically like below:

    1.Create an empty TableLayout in xml like below:

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    </TableLayout>
    

    2.Get the TableLayout reference from xml and create two TableRows each one having TextView children:

    //get the TableLayout
    TableLayout tableLayout = findViewById(R.id.tableLayout);
    
    //create the first TableRow
    TableRow tableRow1 = new TableRow(this);
    tableRow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
    tableRow1.setBackgroundColor(ContextCompat.getColor(this, android.R.color.white));
    
    //add 3 TextViews in TableRow1 and add TableRow1 to TableLayout
    tableRow1.addView(getTextView(android.R.color.holo_purple, android.R.color.black, "Code"));
    tableRow1.addView(getTextView(android.R.color.holo_red_light, android.R.color.black,"Desc"));
    tableRow1.addView(getTextView(android.R.color.holo_green_dark, android.R.color.black,"Status"));
    tableLayout.addView(tableRow1);
    
    //create the second TableRow
    TableRow tableRow2 = new TableRow(this);
    tableRow2.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
    tableRow2.setBackgroundColor(ContextCompat.getColor(this, android.R.color.white));
    
    //add 1 TextView in TableRow2 and add TableRow2 to TableLayout
    tableRow2.addView(getTextView(android.R.color.black, android.R.color.holo_red_light,"No records"));
    tableLayout.addView(tableRow2);
    

    with the below helper function to create each TextView in TableRow programmatically. The key point here is to use layoutParams.weight = 1 on TextView TableRow.LayoutParams to be able to get equal width between all columns in TableRow:

    private TextView getTextView(int backgroundColorId, int textColorId, String text){
    
        TextView tv = new TextView(this);
        tv.setBackgroundColor(ContextCompat.getColor(this, backgroundColorId));
        tv.setTextColor(ContextCompat.getColor(this, textColorId));
        tv.setText(text);
        tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
        int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
        tv.setPaddingRelative(padding, padding, padding, padding);
    
        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
        layoutParams.weight = 1; //this is mandatory to get equal width between all columns in TableRow
        tv.setLayoutParams(layoutParams);
    
        return tv;
    }
    

    Result:

    tablelayout