8

am trying to figure out the max scroll position that the WebView can reach, i've tried the webView.pageDown(true) but the result is delayed ( i cant scroll it down, then up infront of the user, and this method doesn't work every time), i've tried also webView.getContentHeight() and the height isn't correct for Arabic content.

Please Advice

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • Why do you want to know the max scroll position? – springrolls May 29 '12 at 07:21
  • am trying to make pages from `WebView` content, the user must not scroll through the content, we must make it pages, so when i scroll down using `scrollTo` i cant figure if i reached the end or not, this is the requirement – Mohammad Ersan May 29 '12 at 07:32

3 Answers3

16

ok, i figured out the answer

you can get the real content height using

(int) Math.floor(webView.getContentHeight() * webView.getScale());

when you get the real height, then just override the scroll method in webview to listen to scroll event, if the scroll reach the real height, your webview is in the bottom of the scroll.

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { int lenght =(int) Math.floor(WebView1.getContentHeight() * WebView1.getScale()); return false; } How do you come to know by this the page has reached to end....... – Vipin Sahu Jun 15 '12 at 10:28
  • @VipinSahu See my answer to http://stackoverflow.com/questions/10956443/android-making-a-button-visible-once-webview-is-done-scrolling for a comprehensive solution. – karora Apr 28 '13 at 08:43
  • 1
    WebView.getScale is deprecated, we should use getScaleY and getScaleX instead. – shalafi Jul 22 '15 at 11:33
8
@Override
public void onScroll(int l, int t) {
    int height = (int) Math.floor(webView.getContentHeight() * webView.getScale());
    int webViewHeight = webView.getHeight();
    int cutoff = height - webViewHeight - 10; // Don't be too strict on the cutoff point
    if (t >= cutoff) {
        setDisclaimerButtonEnabled(true);
    }
}

The non-strictness is required, is because I found on the Samsung S5 the bottom most scroll value was only 1 pixel value away from the bottom most value!

Meanman
  • 1,474
  • 20
  • 17
-1

Loading / Visible button only when webview reached / scrolled to bottom.

Create JavaScript class :

public class JavaScriptInterface {

  @android.webkit.JavascriptInterface
  public void didScrollToBottom() {
    Log.d(TAG, "Scroll to Bottom");
    myHandler.post(new Runnable() {
      @Override
      public void run() {
         btnAccept.setVisibility(View.VISIBLE);

          }
       });
      }
    }

In onCreate() :

final JavaScriptInterface jsInterface = new JavaScriptInterface();
myWebView.addJavascriptInterface(jsInterface, "AndroidFunction");
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
Satya
  • 528
  • 1
  • 5
  • 9