Search code examples
androidscrollview

android: ScrollView cuts off the top


So I have a layout with a ScrollView. When I test my code, the ViewPager-heigth jumps from a reasonable value (1845) to very small (so fast the eye can't see). If I switch the layout_height of the ViewPager to wrap_content the height jumps to 0.

Aditionally, when the ViewPager has a heigth > 0, the top half of it is outside the Screen and only the bottom half is visible.

Clearly I'm doing something wrong here:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/colorDeadBackground"
    android:fillViewport="true">

<LinearLayout
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:outlineProvider="bounds"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">
        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:elevation="2dp"
            app:cardCornerRadius="6dp">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager_pics"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <com.finder.ViewPagerIndicator.LinePageIndicator
            android:id="@+id/indicator"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="4dp"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            app:strokeWidth="2dp"
            app:unselectedColor="#88888888"
            app:selectedColor="@color/colorWhite"/>

        </androidx.cardview.widget.CardView>
...

If I set the height to a fixed value (either programatically or in the xml) the problem persists, that only the top half of the ViewPager is seen. Even the preview Window says its only half on the screen:

enter image description here


Solution

  • To get rid of the weird offset thing (that the ViewPager starts obove the screen), I had to remove the android:layout_gravity="center" from the Linearlayout. No idea why, if someone explains that to me, that would be dope. Probably has soemthing to do, that I used android:layout_gravity="center" twice in the Linearlayout and in the CardView.

    Then I still ahd to set the height of the ViewPager programatically, so that the height isn't 0. No idea why either, doesn't matter if I changed match_parent to wrap_content anywhere.