SoFunction
Updated on 2025-03-02

How to determine whether to scroll to the bottom of Android WebView

Scene:

Sometimes we need to play some must-read announcements, but you may have to go to it before you can close it after you finish reading it, so you need to scroll to the bottom to display the close button. Announcements are often based on rich text. So when displaying them on Android, WebView must be used. Based on this requirement, you can judge whether the WebView is scrolling to the bottom.

analyze:

To determine whether it is at the bottom, let’s first analyze what are the situations. When the html document is loaded into the WebView, there will be two situations.

The html content in the WebView is not filled with, but there is no scroll bar. The height of the html content is higher than that of the WebView control, and there will be a scroll bar, that is, a scroll bar

For Case 1, we can just display the close button without a scroll bar, but for the second case with a scroll bar, we need to calculate the height, namely: html height = WebView height + scroll bar length. Then, it can be judged that the scroll bar has reached the bottom.

accomplish:

html height <= WebView height + scroll bar length can be regarded as reaching the bottom

Note: The control must be initialized to obtain the height, otherwise the obtained height is 0.

The following is the implementation

 x5WebView?.let {
     = object : WebViewClient() {
	    override fun onPageFinished(
	           view: WebView,
	           url: String
	       ) {
	           (view, url)
	           postSafeDelayed(1000) {
	               loading_pb.visibility = 
	           }
	           //After the page loading, determine whether it reaches the bottom logic	            {
	                (0, 0)
	                val htmlContentHeight: Int = ()
	                val viewHeight: Int = ()
	                //If the html height is less than the control height, it means that the close button can be displayed directly under one screen	                if(htmlContentHeight &lt;= viewHeight){
	                	// TODO displays the close button	                }else{
	                //This means that if there is a scroll bar, you need to listen to the scroll event	                	view?.setOnCustomScrollChangedListener(object : {
                        override fun onScrollChange(
                            scrollX: Int,
                            scrollY: Int,
                            oldScrollX: Int,
                            oldScrollY: Int
                        ) {
                            ("setOnCustomScrollChangedListener Scroll the bottom,htmlhigh:${htmlContentHeight},scrollY:${scrollY}")
                            //It's already at the bottom                            if(htmlContentHeight - (viewHeight + scrollY) &lt;= 0){
                                ("setOnCustomScrollChangedListener reaches the bottom")
                                //TODO reaches the bottom                            }
                        }
                    })
	                }
                
				}
	       }
	   	}
  }
}

The above is the implementation. This event is defined on the WebView. It listens to the custom listening under the onScrollChanged method of the WebView.

Summarize

This is the article about how to determine whether Android WebView scrolls to the bottom. For more information about Android WebView scrolling to the bottom, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!