Search code examples
androidlayoutscrolltablelayout

Scroll through tabellayout doesn't work in Android


I'm trying to make my tablelayout which is nested in a relativeview scrollable. Tried a couple of tutorials but nothing worked. Here is the xml and my java code:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/white" android:padding="10px"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/textView1"
    android:paddingBottom="10dp" android:text="Mijn Rooster"
    android:textSize="20sp" android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/tablelayout"></TextView>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <TableLayout android:id="@+id/tablelayout"
        android:layout_width=
"fill_parent" android:layout_height="wrap_content"
        android:stretchColumns="0" android:layout_below="@+id/textView1"
        android:layout_above="@+id/btnsearch">

    </TableLayout>
</ScrollView>

<Button android:id="@+id/btnsearch" android:layout_width="wrap_content"
    android:text="Zoek op Datum" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"></Button>
</RelativeLayout>

And here is the JAVA code:

TableLayout tl = (TableLayout) findViewById(R.id.tablelayout);

    if (date_selected == null) {
        for (int i = 0; i < roostermap.size(); i++) {

            TableRow trdaydatetime = new TableRow(this);
            TableRow trinfo = new TableRow(this);

            trdaydatetime.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            rowSpanLayout.span = 3;

            TextView txtDay = new TextView(this);
            TextView txtTime = new TextView(this);
            TextView txtResult = new TextView(this);

            // Set Text
            txtDay.setText(roostermap.get(i).getDay(
                    roostermap.get(i).getRoster_date().getDay(),
                    roostermap.get(i).getRoster_date())
                    + " " + df.format(roostermap.get(i).getRoster_date()));
            txtTime.setText(dft.format(roostermap.get(i).getRoster_start())
                    + " - " + dft.format(roostermap.get(i).getRoster_end()));
            txtResult.setText(roostermap.get(i).getWorkplace_name());

            // Day&Date Layout
            txtDay.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtDay.setPadding(5, 5, 0, 0);
            txtDay.setTextColor(Color.BLACK);

            // Text Time layout
            txtTime.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtTime.setTextColor(Color.BLACK);
            txtTime.setPadding(0, 5, 10, 0);
            txtTime.setGravity(Gravity.RIGHT);

            // Text Result layout
            txtResult.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtResult.setPadding(5, 0, 0, 5);

            trdaydatetime.addView(txtDay);
            trdaydatetime.setBackgroundResource(R.drawable.trup);
            trdaydatetime.addView(txtTime);
            trinfo.addView(txtResult, rowSpanLayout);
            trinfo.setBackgroundResource(R.drawable.trdown);

            tl.addView(trdaydatetime, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            tl.addView(trinfo, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }

Solution

  • In order for the 'scrolling' to work you need the content of the ScrollView to be larger than the ScrollView itself. Since you have set the android:layout_height value to wrap_content, the height of the ScrollView matches the height of its content - which means no scrolling.

    Fix the android:layout_height value of the ScrollView by either setting it to fill_parent (possibly with android:layout_weight) or a pixel value.