Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView getContentHeight() doesnt return the correct value

I have a requirement where I display a WebView with a checkbox beneath it. The checkbox is initially in disabled state. Only when the user scrolls down to the bottom of the webview , the checkbox is enabled. I do this by extending Webview class and writing my onScrollChanged listener as below:

@Override
protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) {
     if ( (getContentHeight() - (top + getHeight())) <= mMinDistance )
     {
         Log.i("MYACT","content height: "+getContentHeight()+" top: "+top+" Height: "+getHeight()+" minDistance: "+mMinDistance);
         Log.i("MYACT", "Reached bottom");
         mOnBottomReachedListener.onBottomReached(this);
     }
     else{
         Log.i("MYACT","content height: "+getContentHeight()+" top: "+top+" Height: "+getHeight()+" minDistance: "+mMinDistance);
         Log.i("MYACT", "Not at bottom");
         mOnBottomReachedListener.onNotAtBottom(this);
     }
     super.onScrollChanged(left, top, oldLeft, oldTop);
}

The problem is the condition

(getContentHeight() - (top + getHeight())) <= mMinDistance) //mMinDistance is 0 in my case

is satisfied even before i scroll down to the bottom of the page. As you can see, I tried printing the values of each in log and found that the parameter "top" sometimes exceeds the value "getContentHeight".

Sample log values:

08-14 11:28:19.401: I/MYACT(1075): content height: 3020 top: 3861 Height: 416 minDistance: 0

How can i make avoid this scenario in this case? Should i use a different way of checking whether i have scrolled down to the bottom of the page?

Thanks

like image 266
CuriousCoder Avatar asked Aug 14 '13 15:08

CuriousCoder


1 Answers

I found the issue with this after some research. Webview usually scales the web page based on the screen size and then renders it. It usually zooms the page so that it doesn't look too small in the phone. You can get this scale factor by using the method getScale()

So, in my case, i would use the following condition to check whether end of page has been reached

(getContentHeight()*getScale() - (top + getHeight())) <= mMinDistance) //mMinDistance is 0 in my case

like image 162
CuriousCoder Avatar answered Nov 15 '22 08:11

CuriousCoder