1

I have this xml file for my activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner_strassenfuehrer_fb_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
        android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" >

        <TableRow
            android:scrollbars="vertical"
            android:background="#A6A6A6"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Störcode" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Bezeichnung" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Letztes Auftreten" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Anlagen ID" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Projekt" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Benötigte Ersatzteile" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Benötigtes Werkzeug" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Handlungsleitfaden" />

        </TableRow>
    </TableLayout>

And my code is:

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

    //read database and put values dynamically into table
    tableLayout = (TableLayout) findViewById(R.id.tablelayout_strassenfuehrer_fb_screen);
    for (int i = 0; i < 100; i++) {

        TableRow row = new TableRow(this);
        row.canScrollVertically(1);

        //column 1
        TextView tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 2
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 3
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 4
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 5
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 6
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 7
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 8
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        tableLayout.addView(row, i + 1);
    }
}

I am new in android and I would like to make the rows scrollable, but not the "first" row, from my xml-layout.

Also I would like to fit the elements, which I put dynamically into my TableLayout because the elements not fit on the column.

I am new in android and would be happy, if someone can help me out :)

Thank you

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Josi Allen
  • 137
  • 2
  • 10

1 Answers1

0

Though this is not a nice solution, I would suggest to keep two separate TableLayout. One for the header and the other for the scrolling. So the final layout will look like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Spinner
        android:id="@+id/spinner_strassenfuehrer_fb_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <TableRow
            android:background="#A6A6A6"
            android:padding="5dp"
            android:scrollbars="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Störcode"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Bezeichnung"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Letztes Auftreten"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Anlagen ID"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Projekt"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Benötigte Ersatzteile"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Benötigtes Werkzeug"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Handlungsleitfaden"
                android:textAllCaps="false" />

        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" />
</LinearLayout>

Just get the header by id header and then populate header first. Then you add views to the second TabLayout. I hope you get the idea.

However, I would highly suggest to implement this using GridView or RecyclerView with GridLayoutManager.

Update

This is just a sample code. The code will not work I guess. I just put a sample to give you an idea.

tableLayout = (TableLayout) findViewById(R.id.tablelayout_strassenfuehrer_fb_screen);

tableLayoutHeader = (TableLayout) findViewById(R.id.header);

for (int i = 0; i < 100; i++) {

    TableRow row = new TableRow(this);
    row.canScrollVertically(1);

    //column 1
    TextView tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 2
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 3
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 4
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 5
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 6
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 7
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 8
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    if(i == 0) tableLayoutHeader.addView(row, i + 1);
    else tableLayout.addView(row, i + 1);
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98