Search code examples
androidscrollview

toggle visibility on scroll of scrollview android


I have a scrollview and inside there's a button. I want to set the visibility of the button to GONE when the scrollview is scrolled. I tried scrollview.setOnScrollChangeListener() but it's saying my minimum sdk support has to be 23 (but I want 17). If i set it to 23 , it works fine. How do I make it work with minimum sdk 17? Here's my code:

XML:

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

JAVA:

rootFull.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                showComments.setVisibility(View.GONE);

            }
        });

Solution

  • Try this for api lower than 17

     rootFull.getViewTreeObserver().
    addOnScrollChangedListener(new 
    ViewTreeObserver.OnScrollChangedListener()
     {
        @Override
        public void onScrollChanged() {
           showComments.setVisibility(View.GONE); 
        }
    });