Issue
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?
Solution
I suspect that mapWebView.loadUrl("file:///android_asset/maps/map.html");
is asynchronous, so mapWebView.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:
mapWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mapWebView.scrollTo(300, 300);
}
}
);
Hope this helps
EDIT: Turns out this is unreliable, use this instead:
mapWebView.setPictureListener(new PictureListener() {
@Override
public void onNewPicture(WebView view, Picture picture) {
mapWebView.scrollTo(300, 300);
}
});
Answered By - Dean Wild
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.