Android: Get the scroll position of a WebView

androidwebview

I'm new to Android.

I would like to simply know where on a page the user has scrolled. When a certain point on the web page appears at the bottom of the screen, I want to trigger an event. But this code causes an exception. I know that WebView inherits getScrollY() from View. Am I not implementing it correctly?

Thanks in advance.

public class Scroll extends Activity {

    public WebView webview;
    public float yPos;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WebView webview = new WebView(this);
        setContentView(webview);
        webview.loadUrl("file:///android_asset/Scroll.html");
    }


    public boolean onTouchEvent(final MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            yPos = webview.getScrollY();
            Log.v("Scroll", "yPos = " + yPos); 
        }
        return false;
    }
}

Best Answer

And the exception is?
Here is a note on WebView screen heights from one of my apps:

// current position (top) = getScrollY();  
// max position = getContentHeight();  
// screen height = getHeight();