Search code examples
androidscrollviewonscrolllistener

How to check if scroll view is scroll to bottom android studio


I have Scrollview cover many element inside. And I want to check if scrollview is scroll to the bottom of scrool. How to implement that. please any one recomment me. Thanks.

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:showDividers="end|middle" >
                <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="
                 text.......
               " />
               <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="
                  text.......
              " />
        </LinearLayout>
    </ScrollView>

Solution

  • Create a custom scrollview as follow:

    public class CustomScrollView extends ScrollView { OnBottomReachedListener mListener;
    
        public CustomScrollView(Context context, AttributeSet attrs,
                            int defStyle) {
        super(context, attrs, defStyle);
        }
    
        public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        }
    
        public CustomScrollView(Context context) {
        super(context);
        }
    
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View view = getChildAt(getChildCount() - 1);
        int diff = (view.getBottom() - (getHeight() + getScrollY())) -  view.getPaddingBottom();
    
        if (diff <= 0 && mListener != null) {
            mListener.onBottomReached();
        }
    
        super.onScrollChanged(l, t, oldl, oldt);
        }
    
        // Getters & Setters
    
        public OnBottomReachedListener getOnBottomReachedListener() {
        return mListener;
        }
    
        public void setOnBottomReachedListener(
            OnBottomReachedListener onBottomReachedListener) {
        mListener = onBottomReachedListener;
        }
    
        //Event listener.
    
        public interface OnBottomReachedListener {
        public void onBottomReached();
        }
    }
    

    In your main activity:

    CustomScrollView scrollView = (CustomScrollView) findViewById(R.id.scrollView);
    
    scrollView.setOnBottomReachedListener(new CustomScrollView.OnBottomReachedListener() {
    
            @Override
            public void onBottomReached() {
                // ScrollView Reached bottom
            }
        });
    

    In your xml file:

        <CustomScrollView 
            android:id="@+id/scrollView" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" >
    
        </CustomScrollView>