Question

I am having trouble in getting a ScrollView to scroll with a TableLayout that has its TableRows added via code. For some reason if I use a HorizontalScrollView this will scroll horizontally but not vertically (obviously) whereas I need both vertical and horizontal. However using a ScrollView with horizontal and vertical scrollbars does neither.

I have defined a ScrollView and a TableLayout in the following xml,

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/ScrollView1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:scrollbars="horizontal|vertical" >

 <TableLayout
    android:id="@+id/table_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
 </TableLayout>
</ScrollView>

I then add tablerows to the tablelayout as follows,

    table = (TableLayout) findViewById(R.id.table_id);
    for (int i = 0; i < 9; i++) {
        TextView headerRow = new TextView(getApplicationContext());

        // set up the keyboard so it doesn't go fullscreen
        //firstEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

        switch (i) {

        case 0:
            headerRow.setText("B/Sight");
            break;

        case 1:
            headerRow.setText("Int/Sight");
            break;

        case 2:
            headerRow.setText("F/Sight");
            break;

        case 3:
            headerRow.setText("Rise");
            break;

        case 4:
            headerRow.setText("Fall");
            break;

        case 5:
            headerRow.setText("Reduced Level");
            break;

        case 6:
            headerRow.setText("Lat");
            break;

        case 7:
            headerRow.setText("Long");
            break;

        case 8:
            headerRow.setText("Remark");
            break;
        }

        setCellProperties(headerRow);
        firstRow.addView(headerRow);

    }

    table.addView(firstRow);

Where setCellProperties() is,

    void setCellProperties(TextView txtView) {
    txtView.setPadding(30, 15, 30, 15);
    txtView.setTextColor(Color.BLACK);
    txtView.setBackgroundResource(R.drawable.cell_shape);
    txtView.setGravity(Gravity.CENTER);
}

I then add more rows with data using the same procedure as above. I have tried wrapping this ScrollView in another Layout such as a LinerLayout as suggested on some other questions, but I can't crack it. It is probably worth mentioning that this will be viewed in Landscape not Portrait.

Was it helpful?

Solution 2

Thanks for the input Engr but after messing around some more the following seemed to fix it.

   <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:scrollbars="horizontal|vertical" >

         <ScrollView
             android:id="@+id/scrollview"
             android:layout_width="wrap_content"
             android:layout_height="match_parent" >

             <TableLayout
                 android:id="@+id/table_id"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content">
             </TableLayout>
         </ScrollView>
  </HorizontalScrollView>

OTHER TIPS

Try That:

    <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout1"
    android:background="#000000" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/linearLayout1" >
        </TableLayout>
    </RelativeLayout>
</ScrollView>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top