Android TableView Tutorial.

TableView is an android library that allows us create and work with Tables in android.

It contains a simple TableView and an advanced SortableTableView.

There is both a free and a premium version of tableView.

 

Requirements of a TableView

TableView requires Android Minimum SDK version of 11 and Compule SDK Version of 25.

Installing TableView

TableView can be installed by adding the following implementation statement in your app leve build.gradle:

implementation 'de.codecrafters.tableview:tableview:2.8.0'

Working with TableView

This involves two processes:

  1. Addding the TableView Element in Your Layout:
<de.codecrafters.tableview.TableView
    
    android_id="@+id/tableView"
    android_layout_width="match_parent"
    android_layout_height="match_parent"
    table_tableView_columnCount="4" />

You can modify the number of columns you want your android table to have.

  1. Referencing the TableView:
    Then you reference the TableView in your Java code:

    TableView tableView = (TableView) findViewById(R.id.tableView);

    Here you can also modify the number of columns you want:

    tableView.setColumnCount(4);

Handling Column Widths

TableView allows you modify the column widths in various manners:

  1. Absolutely Using TableColumnDpWidthModel or TableColumnPxWidthModel
    Here’s an example with TableColumnDpWidthModel:

    TableColumnDpWidthModel columnModel = new TableColumnDpWidthModel(context, 4, 200);
    columnModel.setColumnWidth(1, 300);
    columnModel.setColumnWidth(2, 250);
    tableView.setColumnModel(columnModel);

    And here’s one with TableColumnPxWidthModel:

    TableColumnPxWidthModel columnModel = new TableColumnPxWidthModel(4, 350);
    columnModel.setColumnWidth(1, 500);
    columnModel.setColumnWidth(2, 600);
    tableView.setColumnModel(columnModel);
  2. Relatively with the TableColumnWeightModel
    The defauly column weight is 1.

    TableColumnWeightModel columnModel = new TableColumnWeightModel(4);
    columnModel.setColumnWeight(1, 2);
    columnModel.setColumnWeight(2, 2);
    tableView.setColumnModel(columnModel);

Showing Data

Data can be shown easily in TableView with help of SimpleTableDataAdapter class.

This allows us easily render two-dimensional string array in a tabular format.

This adapter will turn the strings you supply into TextViews and display them inside TableView at the same position as previous in the 2D-String-Array.

Here’s an example:

public class MainActivity extends AppCompatActivity {

    private static final String[][] DATA_TO_SHOW = { { "This", "is", "a", "test" }, 
                                                     { "and", "a", "second", "test" } };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TableView<String[]> tableView = (TableView<String[]>) findViewById(R.id.tableView);
        tableView.setDataAdapter(new SimpleTableDataAdapter(this, DATA_TO_SHOW));
    }
}