2

I have a webView, where I display a HTML I fetch from the backend.
My problem is that I am trying to display a loading message, and show the content only after the page is done. For doing that, I tried to use onPageFinished, which winda works, but not entirely, because it is called after the data is fetched, but BEFORE the page is displayed, so I'm still displaying a blank screen for about 1 second, before finally displaying the HTML.

From the oficial docs:

public void onPageFinished (WebView view, String url)

Added in API level 1 Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).

The problem is that onNewPicture(WebView, Picture) is deprecated

So my question is, is there a way to know when the page is finished, but also fully displayed?

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.messageId = this.getIntent().getStringExtra(ITEM_ID_KEY);

    this.setUpActionBar();
    this.setContentView(R.layout.activity_inbox_item_detail);
    WebView view = (WebView) this.findViewById(R.id.webview);
    view.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            view.setVisibility(View.VISIBLE);
        }
    });
    this.fetchInboxDetailsTask(this.messageId);
}
arlistan
  • 731
  • 9
  • 20
  • One of thia guys http://stackoverflow.com/questions/3149216/how-to-listen-for-a-webview-finishing-loading-a-url-in-android says that u must set a new WebViewClient and override that method you said there. – eduyayo Sep 30 '14 at 21:40
  • Try to override window.onload / onloadend by evaluating Javascript in Webview page. Check out global events https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers – Muhammed Yalçın Kuru Jun 17 '19 at 16:59

2 Answers2

0

I have a WebView in a fragment and, just for your reference, I have set it up like this.

May be you are missing something. "onPageFinished" works just fine for me.

In onViewCreated method:

        mWebView = (WebView) view.findViewById(R.id.appWebView);
        mWebView.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url)
            {
                webViewProgressBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }
        });

and then:

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.loadUrl("http://play.google.com/store/apps/");
Sheraz Nadeem
  • 311
  • 2
  • 7
0

You may consider using the Advanced Webview Library for android it provides an interface to reaspond to when page start loading and when finishes! here (AdvancedWebView)

Xenolion
  • 12,035
  • 7
  • 33
  • 48