Search code examples
androidscrollviewscreen-orientationandroid-scrollviewscreen-rotation

Android ScrollView not filling full width of screen after rotating


My app is working fine and there is just one issue: When the screen is held horizontally, the ScrollView does not fill the parent. The part of the xml is:

<HorizontalScrollView
    android:id="@+id/hsvShowDay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff00ff00"
    android:padding="5dp" >

    <ScrollView
        android:id="@+id/svShowDay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff0000"
        android:padding="5dp" >

        <TableLayout
            android:id="@+id/tlShowDay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="true" >

            <TableRow
                android:id="@+id/trEmptyRow" >                    
            </TableRow>

        </TableLayout>

    </ScrollView>

</HorizontalScrollView>

I have already tried setting

android:fillViewport=true

as well as resetting the layout parameters in the onCreate method like so:

ScrollView svCalendar = (ScrollView)findViewById(R.id.svShowDay);
ScrollView.LayoutParams params = new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,ScrollView.LayoutParams.MATCH_PARENT);
svCalendar.setLayoutParams(params);

Can anyone tell me, what I am doing wrong? Is there anything I need to change? Thank you very much in advance!


Solution

  • Today morning I tried adding android:fillViewport="true" to the ScrollView again. It still didn't work, but as both answers suggested that I add it, I tried adding it to the HorizontalScrollView and with that my app finally gets the functionality that I expected.

    So in short the answer is to add android:fillViewport="true" to the HorizontalScrollView.

    PS: When I also add android:fillViewport="true" to the ScrollView, then the TableLayout gets centered in the middle of my ScrollView. (This is not needed for my app, but it could help out someone else one day.)

    Thanks for the two answers that pushed me in the right direction!