Search code examples
androidscrollview

How to do actions when scroll view reached bottom end?


I have an Activity with a scroll view and 2 buttons, When open this activity both buttons are disabled. What I want is, when I reach the end of the scroll view, enable both buttons to user to accept or decline the content. How can I do this in simple way.

Button before enabled

image

Thanks in advance.


Solution

  • I have done this things with webView here is my code, hope it will help you.

    webView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() {
                    @Override
                    public void onScroll(int l, int t) {
                        int tek = (int) Math.floor(webView.getContentHeight() * webView.getScale());
    
                        if (tek - webView.getScrollY() <= webView.getHeight() + 5) {
                            // enable your button 
                        }
    
                    }
                });
    

    this is the class

    public class ObservableWebView extends WebView
    {
        private OnScrollChangedCallback mOnScrollChangedCallback;
    public ObservableWebView(final Context context)
    {
        super(context);
    }
    
    public ObservableWebView(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
    }
    
    public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt)
    {
        super.onScrollChanged(l, t, oldl, oldt);
        if(mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
    }
    
    public OnScrollChangedCallback getOnScrollChangedCallback()
    {
        return mOnScrollChangedCallback;
    }
    
    public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback)
    {
        mOnScrollChangedCallback = onScrollChangedCallback;
    }
    
    /**
     * Impliment in the activity/fragment/view that you want to listen to the webview
     */
    public static interface OnScrollChangedCallback
    {
        public void onScroll(int l, int t);
    }
    

    }

    happy coding.