Home > database >  How insert fixed footer outside scroll view in constrain layout inside scrollview
How insert fixed footer outside scroll view in constrain layout inside scrollview

Time:12-03

I want to insert fixed footer bottom of the screen and above should be content in scroll view I used below code but it's throwing exception ScrollView can host only one direct child

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="mainActivity"
            type="com.example.footer.MainActivity" />
    </data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@ id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </ScrollView>

    <TextView
        android:id="@ id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

CodePudding user response:

Try is, it might be what you're looking for

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

<!--        ScrollView can only contain 1 view inside-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@ id/footer"
            app:layout_constraintTop_toTopOf="parent">

<!--            Use LinearLayoutCompat or ConstraintLayout or layout any-->
            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent">

                <Button
                    android:id="@ id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            <!-- do some thing -->
            </androidx.appcompat.widget.LinearLayoutCompat>

        </ScrollView>

        <TextView
            android:id="@ id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

The constraints are not set correctly. You need to add id for the ScrollView and the TextView.
Then set the TextView top constraint to be bottom of ScrollView: layout_constraintTop_toBottomOf="@ id/scrollviewid"
And ScrollView bottom constraint to be top of TextView:
layout_constraintBottom_toTopOf="@ id/textviewid"

  • Related