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;
}
}
And the exception is?
Here is a note on WebView screen heights from one of my apps:
The WebView has a method to get its content height, which I used in my solution:
Look out for:
For those who are using Ionic framework.
Getting the scrollTop from the Webview < ion-content id="_ionic_content" > does not work, because ion_content wraps it's own scroller.
To get the scrollTop of the webView use :
javascript ->
This has worked for me.
You are writing
WebView webview;
at two places inside the OnCreate() method.Simply write
webview = new WebView(this);
instead ofWebView webview = new WebView(this);
This way you can solved the null pointer issue.