Search code examples
androidandroid-viewpagerscrollviewandroid-linearlayout

Android - How to fill LinearLayout in ScrollView


There's LinearLayout in ScrollView.

I want fill LinearLayout with ViewPager,

but ViewPager's height is 0.

The area below tabLayout of LinearLayout must be filled with ViewPager.

If i set ViewPager's height to specific dp,

it shows blue.

Please help 😭

(Red: ScrollView, Blue: ViewPager)

MainFragment.xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/red"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_weight="0">
            <Button
                android:id="@+id/ATabButton"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="A"
                android:textSize="18sp"
                android:textStyle="bold"
                android:background="@drawable/shape_tab_button_clicked"
                android:stateListAnimator="@null"
                app:layout_constraintWidth_percent="0.5"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toStartOf="@id/reviewTabButton">
            </Button>
            <Button
                android:id="@+id/BTabButton"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="B"
                android:background="@drawable/shape_tab_button_unclicked"
                android:stateListAnimator="@null"
                app:layout_constraintWidth_percent="0.5"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toEndOf="@id/menuTabButton"
                app:layout_constraintEnd_toEndOf="parent">
            </Button>
        </androidx.constraintlayout.widget.ConstraintLayout>
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/contentsViewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/blue"/>
    </LinearLayout>

</ScrollView>

enter image description here


Solution

  • Add fill viewport in ScrollView

    android:fillViewport="true"