1

APP description

I am writing code for android EPub reader.

Book part (html) is loaded into WebView

Problem

After device rotation i am trying to scroll to last position in webview as text.

Data is loaded into webview by loadData(text, ..., ...) because I do not load webpage from URL, but from locally saved html in EPub file.

webView.loadData(text, "text/html; charset=UTF-8", null);

I found out this function is async and i have to wait until data is fully loaded and after that try to scroll.

I have tried adviced solutions such as

final int Y = savedInstanceState.getInt(CURRENT_WEBVIEW_POSITION_Y);

webView.postDelayed(new Runnable() {
    public void run() {
        if (webView.getProgress() == 100) {
            webView.postDelayed(new Runnable() {
                public void run() {
                    webView.scrollTo(0, Y);
                }
            }, 10);
        } else {
            webView.post(this);
        }
    }
}, 10);

or

final int Y = savedInstanceState.getInt(CURRENT_WEBVIEW_POSITION_Y);

while (webView.getProgress() < 100){
    Thread.sleep(50);
}
Thread.sleep(10);
webView.scrollTo(0, Y);

but they did not work.

Y position of webview is stored into and retrieved from bundle correctly.

Johnny
  • 21
  • 5

1 Answers1

1

Problem solved with modification of first code:

webView.postDelayed(new Runnable() {
    @Override
    public void run() {
        if (webView.getContentHeight() > 0){
            webView.scrollTo(0, Y);
            webView.removeCallbacks(this);
        } else {
            webView.postDelayed(this, 10);
        }
    }
}, 10);

Solved here Is there a listener for when the WebView displays it's content?

Community
  • 1
  • 1
Johnny
  • 21
  • 5