0

I cannot able to find a solution to hide Floating Action Button when scrolled on ScrollView. I can find some solutions but all of them are for Java, there is no valid solution for Kotlin.

You can see my layout below:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">


    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <!-- MAIN CONTENT -->


        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floating_action_button"
        style="@style/Widget.MaterialComponents.FloatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="5dp"
        app:backgroundTint="@color/colorPrimary"
        app:fabSize="mini"
        app:srcCompat="@drawable/ic_arrow_downward_white"
        app:tint="#FFFFFF" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69

2 Answers2

1

Simply You can use setOnScrollChangeListener to get scroll position on run time.

An example of code will look like this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            scrollView.setOnScrollChangeListener(object : View.OnScrollChangeListener {
                override fun onScrollChange(v: View?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {

                    val x = scrollY - oldScrollY
                    if (x > 0) {
                        //scroll up
                     //show fab icon 
                    } else if (x < 0) {
                        //scroll down
                      //hide fab icon
                    } else {

                    }
                }

            })
}
0

This is the java version convert it to kotlin version.

    mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        final static int SHOW_THRESHOLD = 120;
        float prevY = 0;

        @Override
        public void onScrollChanged() {
            //LOGI(TAG, "Scroll Y = " + mScrollView.getScrollY());
            float scrollY = mScrollView.getScrollY();

            if (fab != null) {
                if (mScrollView.getScrollY() > 0) {
                    fab.hide();
                } else {
                    fab.show();
                }
            }

            if (scrollY > SHOW_THRESHOLD && mControlsVisible) {
                onHide();
                mControlsVisible = false;
            } else if (scrollY == 0 && !mControlsVisible) {
                onShow();
                mControlsVisible = true;
            }
        }
    });
Mahabub Karim
  • 810
  • 11
  • 17