I'm trying to use scrollTo method of webview. This my layout file for the webview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/webMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
What I'm trying to do is showing a html file (which has only a map image) and scrolling to certain area on image with :
mapWebView.loadUrl("file:///android_asset/maps/map.html");
mapWebView.scrollTo(300, 300);
But when the webview is loaded it always shows the (0, 0) of the image.
What is the problem with scrollTo()
here?
Try this..:
I suspect that
mapWebView.loadUrl("file:///android_asset/maps/map.html");
is asynchronous, somapWebView.scrollTo(300, 300);
executes before the webview has finished loading. Once the page loads it will have lost the scroll setting you applied and will be reset to the top.You need to listen in for the the page loading and then scroll it:
Hope this helps
EDIT: Turns out this is unreliable, use this instead:
Following override works better for me.