Search code examples
androidxmlbuttonandroid-constraintlayoutfloating-action-button

FloatingActionButton inside constraintLayout in ScrollView


i have a floatingActionButton inside a ConstraintLayout which is inside a ScrollView

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_grey">

    <androidx.constraintlayout.widget.ConstraintLayout>

        ----
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bag_background"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            />
        -----
    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

the problem is that the button is scrolling down and up with the UI

Ps: i don't know if it is important, but the scrollView is inside a drawerLayout


Solution

  • You can make Your root view as FrameLayout then inside it, you will but the ScrollView then the FAB button something like below

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_grey">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_grey">
    
            <androidx.constraintlayout.widget.ConstraintLayout>
    
                <!--  your view xml here -->
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </ScrollView>
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:src="@drawable/bag_background"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            />
    </FrameLayout>