0

I show a TableLayout with Data, the first row is the columns' name and since the second row is data, i need to pin up the first row but i have to be scrollable horizontally with the data(others rows) not vertically, but the data(others rows) just the data, have to be both(horizontally and vertically), because a i need to see always the columns' name.

I make my tablelayout like this:

TableLayout table= (TableLayout) findViewById(R.id.table);

//columns' name (first row)
TableRow column= new TableRow(this);
for(int i=0;i<lenght; i++){
TextView columnName = new TextView(this);
columnName.setText("Column "+i);
column.addView(columnName);
}

table.addView(column); //with this i add the first row with columns' name

//Data
for(int i=0;i<lenght; i++){
TableRow rowData = new TableRow(this);
TextView data= new TextView(this);
data.setText("data "+i);
rowData .addView(data);
table.addView(rowData );//add rows(since the second row)
}

With this structure i scroll all the tablelayout:

->ScrollView
      ->HorizontalScrollView
            ->TableLayout  
  • Possible duplicate of [Android layout - How to implement a fixed/freezed header and column](https://stackoverflow.com/questions/7119231/android-layout-how-to-implement-a-fixed-freezed-header-and-column) – Vikasdeep Singh Aug 30 '18 at 01:33

1 Answers1

0

I fix the header doing this :

<HorizontalScrollView>
    <TableLayout>
        <TableRow>
            <TableLayout>
                <--! Headers -->
            </TableLayout>
        </TableRow>
        <TableRow>
            <ScrollView>
                <TableLayout>
                    <--! Rows/Data -->
                </TableLayout>
            </ScrollView>
        </TableRow>
    </TableLayout>
</HorizontalScrollView>

Obviously if you want it dynamic, you have to do the same but with programming. Something like this

Your xml:
<HorizonalScrollView>
    <TableLayout
     android:"@+id/tableMaster">
    </TableLayout>
</HorizonalScrollView>
-------------------------------------------------------------------------------------

Your jave code:

//it contains the TablesLayout
TableLayout tableFirst = (TableLayout) findViewById(R.id.tableMaster);
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);

//The Header
TableLayout tableSecond = new TableLayout(this);
TableRow rowHeader = new TableRow(this);
tableSecond.addView(rowHeader);

//The Data    
ScrollView scroll = new ScrollView(this);//It makes this table scrollable vertically
TableLayout tableThird = new TableLayout(this);
TableRow rowData = new TableRow(this);
tableThird.addView(rowData);
scroll.addView(tableThird);

//Add the header
row1.addView(tableSecond);
tableFirst.addView(row1);

//Add the data scrollable
row2.addView(scroll);
tableFirst.addView(row2);

You have to put your own logic.